in cfn-resources/network-peering/cmd/resource/resource.go [56:118]
func validateOrCreateNetworkContainer(req *handler.Request, prevModel *Model, currentModel *Model) (*mongodbatlas.Container, error) {
log.Debugf("validateOrCreateNetworkContainer prevModel:%+v, currentModel:%+v", prevModel, currentModel)
var container mongodbatlas.Container
if currentModel.ApiKeys == nil {
return &container, fmt.Errorf("No ApiKeys found in currentModel:%+v", currentModel)
}
if currentModel.ProjectId == nil {
return &container, fmt.Errorf("ProjectId was not set! currentModel:%+v", currentModel)
}
var ar string
if currentModel.AccepterRegionName == nil { // use lambda default
r := os.Getenv("AWS_REGION")
log.Debugf("AccepterRegionName was nil, found AWS_REGION region:%v", r)
ar = util.EnsureAtlasRegion(r)
} else {
r := *currentModel.AccepterRegionName
log.Debugf("AccepterRegionName was SET to:%v", r)
ar = util.EnsureAtlasRegion(r)
}
log.Debugf("converted to atlas region :%v", ar)
projectId := *currentModel.ProjectId
region := &ar
// Check if have AWS container for this group,
// if so return it -
// if passed a ContainerId and it does not match, then
// return an ERROR, explain to remove the ContainerId parameter
found, c, err := findContainer(projectId, *region, currentModel)
if err != nil {
return &container, err
}
if found {
return c, nil
}
// Didn't find one for this AWS region, need to create
log.Debugf("projectId:%v, region:%v, cidr:%+v", projectId, region, &DefaultAWSCIDR)
containerRequest := &mongodbatlas.Container{}
containerRequest.RegionName = *region
containerRequest.ProviderName = "AWS"
containerRequest.AtlasCIDRBlock = DefaultAWSCIDR
log.Debugf("containerRequest:%+v", containerRequest)
client, err := util.CreateMongoDBClient(*currentModel.ApiKeys.PublicKey, *currentModel.ApiKeys.PrivateKey)
if err != nil {
return &container, err
}
containerResponse, resp, err := client.Containers.Create(context.TODO(), *currentModel.ProjectId, containerRequest)
// TODO add logging here
if resp != nil && resp.StatusCode == 409 {
log.Warnf("Container already exists, looking for it: resp:%+v", resp)
found, c, err := findContainer(projectId, *region, currentModel)
if err != nil {
return c, err
}
if found {
return c, nil
}
} else if err != nil {
return &container, err
}
log.Debugf("created container response:%v", containerResponse)
return containerResponse, nil
}