func Update()

in cfn-resources/cluster/cmd/resource/resource.go [405:518]


func Update(req handler.Request, prevModel *Model, currentModel *Model) (handler.ProgressEvent, error) {
	setup()
	log.Debugf("Update() currentModel:%+v", currentModel)
	client, err := util.CreateMongoDBClient(*currentModel.ApiKeys.PublicKey, *currentModel.ApiKeys.PrivateKey)
	if err != nil {
		log.Infof("Update - 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", "UPDATING")
	}

	projectID := *currentModel.ProjectId
	clusterName := *currentModel.Name
	log.Debugf("Update - clusterName:%s", clusterName)
	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("Update - 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("Update - error: %+v", err)
			return handler.ProgressEvent{
				OperationStatus:  handler.Failed,
				Message:          err.Error(),
				HandlerErrorCode: cloudformation.HandlerErrorCodeInvalidRequest}, nil
		}
	}

	var autoScaling *mongodbatlas.AutoScaling
	if currentModel.AutoScaling != nil {
		autoScaling = &mongodbatlas.AutoScaling{
			DiskGBEnabled: currentModel.AutoScaling.DiskGBEnabled,
		}
	} else {
		autoScaling = &mongodbatlas.AutoScaling{}
	}

	log.Debugf("Update - autoScaling:%v", autoScaling)
	clusterRequest := &mongodbatlas.Cluster{
		Name:                     cast.ToString(currentModel.Name),
		EncryptionAtRestProvider: cast.ToString(currentModel.EncryptionAtRestProvider),
		ClusterType:              cast.ToString(currentModel.ClusterType),
		BackupEnabled:            currentModel.BackupEnabled,
		DiskSizeGB:               currentModel.DiskSizeGB,
		ProviderBackupEnabled:    currentModel.ProviderBackupEnabled,
		AutoScaling:              autoScaling,
	}
	if currentModel.BiConnector != nil {
		clusterRequest.BiConnector = expandBiConnector(currentModel.BiConnector)
	}
	if currentModel.ProviderSettings != nil {
		clusterRequest.ProviderSettings = expandProviderSettings(currentModel.ProviderSettings)
	}
	if currentModel.ReplicationSpecs != nil {
		clusterRequest.ReplicationSpecs = expandReplicationSpecs(currentModel.ReplicationSpecs)
	}
	if currentModel.ReplicationFactor != nil {
		clusterRequest.ReplicationFactor = cast64(currentModel.ReplicationFactor)
	}

	if currentModel.NumShards != nil {
		clusterRequest.NumShards = cast64(currentModel.NumShards)
	}

	if currentModel.MongoDBMajorVersion != nil {
		clusterRequest.MongoDBMajorVersion = formatMongoDBMajorVersion(*currentModel.MongoDBMajorVersion)
	}

	log.Debugf("Cluster update clusterRequest:%+v", clusterRequest)
	cluster, resp, err := client.Clusters.Update(context.Background(), projectID, clusterName, clusterRequest)
	if err != nil {
		if resp != nil && resp.StatusCode == 404 {
			log.Infof("update 404 err: %+v", err)
			return handler.ProgressEvent{
				Message:          err.Error(),
				OperationStatus:  handler.Failed,
				HandlerErrorCode: cloudformation.HandlerErrorCodeNotFound}, nil
		} else {
			log.Infof("update err: %+v", err)
			code := cloudformation.HandlerErrorCodeServiceInternalError
			if strings.Contains(err.Error(), "not exist") { // cfn test needs 404
				code = cloudformation.HandlerErrorCodeNotFound
			}
			if strings.Contains(err.Error(), "being deleted") {
				code = cloudformation.HandlerErrorCodeNotFound // cfn test needs 404
			}
			return handler.ProgressEvent{
				Message:          err.Error(),
				OperationStatus:  handler.Failed,
				HandlerErrorCode: code}, nil
		}
	}

	return handler.ProgressEvent{
		OperationStatus:      handler.InProgress,
		Message:              fmt.Sprintf("Update Cluster `%s`", cluster.StateName),
		ResourceModel:        currentModel,
		CallbackDelaySeconds: 65,
		CallbackContext: map[string]interface{}{
			"stateName": cluster.StateName,
		},
	}, nil
}