in cfn-resources/cluster/cmd/resource/resource.go [41:207]
func Create(req handler.Request, prevModel *Model, currentModel *Model) (handler.ProgressEvent, error) {
setup()
log.Debugf("Create() currentModel:%+v", currentModel)
client, err := util.CreateMongoDBClient(*currentModel.ApiKeys.PublicKey, *currentModel.ApiKeys.PrivateKey)
if err != nil {
log.Infof("Create - error: %+v", err)
return handler.ProgressEvent{
OperationStatus: handler.Failed,
Message: err.Error(),
HandlerErrorCode: cloudformation.HandlerErrorCodeInvalidRequest}, nil
}
if _, ok := req.CallbackContext["stateName"]; ok {
return validateProgress(client, req, currentModel, "IDLE", "CREATING")
}
projectID := *currentModel.ProjectId
log.Infof("cluster Create projectID=%s", projectID)
if len(currentModel.ReplicationSpecs) > 0 {
if currentModel.ClusterType != nil {
err := errors.New("error creating cluster: ClusterType should be set when `ReplicationSpecs` is set")
log.Infof("Create - error: %+v", err)
return handler.ProgressEvent{
OperationStatus: handler.Failed,
Message: err.Error(),
HandlerErrorCode: cloudformation.HandlerErrorCodeInvalidRequest}, nil
}
if currentModel.NumShards != nil {
err := errors.New("error creating cluster: NumShards should be set when `ReplicationSpecs` is set")
log.Infof("Create - error: %+v", err)
return handler.ProgressEvent{
OperationStatus: handler.Failed,
Message: err.Error(),
HandlerErrorCode: cloudformation.HandlerErrorCodeInvalidRequest}, nil
}
}
// This is the intial call to Create, so inject a deployment
// secret for this resource in order to lookup progress properly
projectResID := &util.ResourceIdentifier{
ResourceType: "Project",
ResourceID: projectID,
}
log.Debugf("Created projectResID:%s", projectResID)
resourceID := util.NewResourceIdentifier("Cluster", *currentModel.Name, projectResID)
log.Debugf("Created resourceID:%s", resourceID)
resourceProps := map[string]string{
"Clust:erName": *currentModel.Name,
}
secretName, err := util.CreateDeploymentSecret(&req, resourceID, *currentModel.ApiKeys.PublicKey, *currentModel.ApiKeys.PrivateKey, &resourceProps)
if err != nil {
log.Infof("Create - CreateDeploymentSecret - error: %+v", err)
return handler.ProgressEvent{
OperationStatus: handler.Failed,
Message: err.Error(),
HandlerErrorCode: cloudformation.HandlerErrorCodeServiceInternalError}, nil
}
log.Infof("Created new deployment secret for cluster. Secert Name = Cluster Id:%s", *secretName)
currentModel.Id = secretName
one := int64(1)
three := int64(3)
replicationFactor := &three
if currentModel.ReplicationFactor != nil {
rf := int64(*currentModel.ReplicationFactor)
replicationFactor = &rf
} else {
log.Debugf("Default setting ReplicationFactor to 3")
}
numShards := &one
if currentModel.NumShards != nil {
ns := int64(*currentModel.NumShards)
numShards = &ns
} else {
log.Debugf("Default setting NumShards to 1")
}
clusterRequest := &mongodbatlas.Cluster{
Name: cast.ToString(currentModel.Name),
EncryptionAtRestProvider: cast.ToString(currentModel.EncryptionAtRestProvider),
ClusterType: cast.ToString(currentModel.ClusterType),
ReplicationFactor: replicationFactor,
NumShards: numShards,
}
if currentModel.BackupEnabled != nil {
clusterRequest.BackupEnabled = currentModel.BackupEnabled
}
if currentModel.ProviderBackupEnabled != nil {
clusterRequest.ProviderBackupEnabled = currentModel.ProviderBackupEnabled
}
if currentModel.DiskSizeGB != nil {
currentModel.DiskSizeGB = clusterRequest.DiskSizeGB
}
if currentModel.MongoDBMajorVersion != nil {
clusterRequest.MongoDBMajorVersion = formatMongoDBMajorVersion(*currentModel.MongoDBMajorVersion)
}
if currentModel.BiConnector != nil {
clusterRequest.BiConnector = expandBiConnector(currentModel.BiConnector)
}
if currentModel.ProviderSettings != nil {
clusterRequest.ProviderSettings = expandProviderSettings(currentModel.ProviderSettings)
}
log.Debugf("DEBUG: clusterRequest.ProviderSettings: %+v", clusterRequest.ProviderSettings)
if currentModel.ReplicationSpecs != nil {
clusterRequest.ReplicationSpecs = expandReplicationSpecs(currentModel.ReplicationSpecs)
}
if currentModel.AutoScaling != nil {
clusterRequest.AutoScaling = &mongodbatlas.AutoScaling{
DiskGBEnabled: currentModel.AutoScaling.DiskGBEnabled,
}
if currentModel.AutoScaling.Compute != nil {
compute := &mongodbatlas.Compute{}
if currentModel.AutoScaling.Compute.Enabled != nil {
compute.Enabled = currentModel.AutoScaling.Compute.Enabled
}
if currentModel.AutoScaling.Compute.ScaleDownEnabled != nil {
compute.ScaleDownEnabled = currentModel.AutoScaling.Compute.ScaleDownEnabled
}
if currentModel.AutoScaling.Compute.MinInstanceSize != nil {
compute.MinInstanceSize = *currentModel.AutoScaling.Compute.MinInstanceSize
}
if currentModel.AutoScaling.Compute.MaxInstanceSize != nil {
compute.MaxInstanceSize = *currentModel.AutoScaling.Compute.MaxInstanceSize
}
clusterRequest.AutoScaling.Compute = compute
}
}
log.Debugf("DEBUG: clusterRequest: %+v", clusterRequest)
cluster, _, err := client.Clusters.Create(context.Background(), projectID, clusterRequest)
if err != nil {
log.Infof("Create - Cluster.Create() - error: %+v", err)
return handler.ProgressEvent{
OperationStatus: handler.Failed,
Message: err.Error(),
HandlerErrorCode: cloudformation.HandlerErrorCodeInvalidRequest}, nil
}
currentModel.StateName = &cluster.StateName
log.Debugf("Created cluster currentModel: %+v", currentModel)
event := handler.ProgressEvent{
OperationStatus: handler.InProgress,
Message: fmt.Sprintf("Create Cluster `%s`", cluster.StateName),
ResourceModel: currentModel,
CallbackDelaySeconds: 65,
CallbackContext: map[string]interface{}{
"stateName": cluster.StateName,
"projectId": projectID,
"clusterName": *currentModel.Name,
"deploymentSecret": secretName,
},
}
log.Debugf("Create() return event:%+v", event)
return event, nil
}