func Read()

in cfn-resources/cluster/cmd/resource/resource.go [210:402]


func Read(req handler.Request, prevModel *Model, currentModel *Model) (handler.ProgressEvent, error) {
	setup()
	log.Debugf("Read() currentModel:%+v", currentModel)

	// Callback is not called - Create() and Update() get recalled on
	// long running operations
	callback := map[string]interface{}(req.CallbackContext)
	log.Debugf("Read -  callback: %v", callback)
	if currentModel.Id == nil {
		err := errors.New("No Id found in currentModel")
		log.Infof("Read - error: %+v", err)
		return handler.ProgressEvent{
			OperationStatus:  handler.Failed,
			Message:          err.Error(),
			HandlerErrorCode: cloudformation.HandlerErrorCodeNotFound}, nil
	}
	secretName := *currentModel.Id
	log.Infof("Read for Cluster Id/SecretName:%s", secretName)
	key, err := util.GetApiKeyFromDeploymentSecret(&req, secretName)
	if err != nil {
		log.Infof("Read - error: %+v", err)
		return handler.ProgressEvent{
			OperationStatus:  handler.Failed,
			Message:          err.Error(),
			HandlerErrorCode: cloudformation.HandlerErrorCodeNotFound}, nil
	}
	log.Debugf("key:%+v", key)

	client, err := util.CreateMongoDBClient(key.PublicKey, key.PrivateKey)
	if err != nil {
		log.Infof("Read - error: %+v", err)
		return handler.ProgressEvent{
			OperationStatus:  handler.Failed,
			Message:          err.Error(),
			HandlerErrorCode: cloudformation.HandlerErrorCodeInvalidRequest}, nil
	}
	// currentModel is NOT populated on the Read after long-running Cluster create
	// need to parse pid and cluster name from Id (deployment secret name).

	//projectID := *currentModel.ProjectId
	//clusterName := *currentModel.Name

	// key.ResourceID should == *currentModel.Id
	id, err := util.ParseResourceIdentifier(*currentModel.Id)
	if err != nil {
		log.Infof("Read - error: %+v", err)
		return handler.ProgressEvent{
			OperationStatus:  handler.Failed,
			Message:          err.Error(),
			HandlerErrorCode: cloudformation.HandlerErrorCodeNotFound}, nil
	}
	log.Debugf("Parsed resource identifier: id:%+v", id)
	log.Debugf("parent --->%+v", id.Parent)

	projectID := id.Parent.ResourceID
	clusterName := id.ResourceID

	log.Debugf("Got projectID:%s, clusterName:%s, from id:%+v", projectID, clusterName, id)
	cluster, resp, err := client.Clusters.Get(context.Background(), projectID, clusterName)
	if err != nil {
		if resp != nil && resp.StatusCode == 404 {
			log.Infof("error 404- err:%+v resp:%+v", err, resp)
			return handler.ProgressEvent{
				Message:          err.Error(),
				OperationStatus:  handler.Failed,
				HandlerErrorCode: cloudformation.HandlerErrorCodeNotFound}, nil
		} else {
			log.Infof("error cluster get- err:%+v resp:%+v", err, resp)
			return handler.ProgressEvent{
				Message:          err.Error(),
				OperationStatus:  handler.Failed,
				HandlerErrorCode: cloudformation.HandlerErrorCodeServiceInternalError}, nil
		}
	}
	if currentModel.AutoScaling != nil {
		currentModel.AutoScaling = &AutoScaling{
			DiskGBEnabled: cluster.AutoScaling.DiskGBEnabled,
		}
		if currentModel.AutoScaling.Compute != nil {
			compute := &Compute{
				Enabled:          cluster.AutoScaling.Compute.Enabled,
				ScaleDownEnabled: cluster.AutoScaling.Compute.ScaleDownEnabled,
				MinInstanceSize:  &cluster.AutoScaling.Compute.MinInstanceSize,
				MaxInstanceSize:  &cluster.AutoScaling.Compute.MaxInstanceSize,
			}
			currentModel.AutoScaling.Compute = compute
		}

	}

	if currentModel.BackupEnabled != nil {
		currentModel.BackupEnabled = cluster.BackupEnabled
	}

	if currentModel.ProviderBackupEnabled != nil {
		currentModel.ProviderBackupEnabled = cluster.ProviderBackupEnabled
	}

	if currentModel.ClusterType != nil {
		currentModel.ClusterType = &cluster.ClusterType
	}
	if currentModel.DiskSizeGB != nil {
		currentModel.DiskSizeGB = cluster.DiskSizeGB
	}
	if currentModel.EncryptionAtRestProvider != nil {
		currentModel.EncryptionAtRestProvider = &cluster.EncryptionAtRestProvider
	}
	if currentModel.MongoDBMajorVersion != nil {
		currentModel.MongoDBMajorVersion = &cluster.MongoDBMajorVersion
	}

	if cluster.NumShards != nil {
		currentModel.NumShards = castNO64(cluster.NumShards)
	}

	currentModel.MongoDBVersion = &cluster.MongoDBVersion
	currentModel.MongoURI = &cluster.MongoURI
	currentModel.MongoURIUpdated = &cluster.MongoURIUpdated
	currentModel.MongoURIWithOptions = &cluster.MongoURIWithOptions
	currentModel.Paused = cluster.Paused
	currentModel.SrvAddress = &cluster.SrvAddress
	currentModel.StateName = &cluster.StateName

	if currentModel.BiConnector != nil {
		currentModel.BiConnector = &BiConnector{
			ReadPreference: &cluster.BiConnector.ReadPreference,
			Enabled:        cluster.BiConnector.Enabled,
		}
	}
	currentModel.ConnectionStrings = &ConnectionStrings{
		Standard:    &cluster.ConnectionStrings.Standard,
		StandardSrv: &cluster.ConnectionStrings.StandardSrv,
		Private:     &cluster.ConnectionStrings.Private,
		PrivateSrv:  &cluster.ConnectionStrings.PrivateSrv,
		//AwsPrivateLink:         &cluster.ConnectionStrings.AwsPrivateLink,
		//AwsPrivateLinkSrv:      &cluster.ConnectionStrings.AwsPrivateLinkSrv,
	}
	log.Debugf("step 2 cluster:+%v", cluster)

	/*
			if cluster.ProviderSettings != nil {
		        ps := &ProviderSettings{
					BackingProviderName: &cluster.ProviderSettings.BackingProviderName,
					DiskIOPS:            castNO64(cluster.ProviderSettings.DiskIOPS),
					EncryptEBSVolume:    cluster.ProviderSettings.EncryptEBSVolume,
					InstanceSizeName:    &cluster.ProviderSettings.InstanceSizeName,
					VolumeType:          &cluster.ProviderSettings.VolumeType,
				}
		        rn := util.EnsureAWSRegion(cluster.ProviderSettings.RegionName)
		        ps.RegionName = &rn
		        if currentModel.ProviderSettings.AutoScaling != nil {
		            ps.AutoScaling = &AutoScaling{
		                DiskGBEnabled: cluster.ProviderSettings.AutoScaling.DiskGBEnabled,
		            }
		            if currentModel.ProviderSettings.AutoScaling.Compute != nil {
		                compute := &Compute{}

		                if currentModel.ProviderSettings.AutoScaling.Compute.Enabled != nil {
		                    compute.Enabled = cluster.ProviderSettings.AutoScaling.Compute.Enabled
		                }
		                if currentModel.ProviderSettings.AutoScaling.Compute.ScaleDownEnabled != nil {
		                    compute.ScaleDownEnabled = cluster.ProviderSettings.AutoScaling.Compute.ScaleDownEnabled
		                }
		                if currentModel.ProviderSettings.AutoScaling.Compute.MinInstanceSize != nil {
		                    compute.MinInstanceSize = &cluster.ProviderSettings.AutoScaling.Compute.MinInstanceSize
		                }
		                if currentModel.ProviderSettings.AutoScaling.Compute.MaxInstanceSize != nil {
		                    compute.MaxInstanceSize = &cluster.ProviderSettings.AutoScaling.Compute.MaxInstanceSize
		                }
		                log.Debugf("compute -- what?> +%v",compute)
		                ps.AutoScaling.Compute = compute
		            }
		        }

		        currentModel.ProviderSettings = ps
			}

		    if currentModel.ReplicationSpecs != nil {
			    currentModel.ReplicationSpecs = flattenReplicationSpecs(cluster.ReplicationSpecs)
		    }

			if currentModel.ReplicationFactor != nil {
			    currentModel.ReplicationFactor = castNO64(cluster.ReplicationFactor)
		    }
	*/
	log.Debugf("Read() end currentModel:%+v", currentModel)

	return handler.ProgressEvent{
		OperationStatus: handler.Success,
		Message:         "Read Complete",
		ResourceModel:   currentModel,
	}, nil
}