func Create()

in cfn-resources/network-peering/cmd/resource/resource.go [121:189]


func Create(req handler.Request, prevModel *Model, currentModel *Model) (handler.ProgressEvent, error) {
	setup()
	client, err := util.CreateMongoDBClient(*currentModel.ApiKeys.PublicKey, *currentModel.ApiKeys.PrivateKey)
	if err != nil {
		log.Warnf("Create - error err:%+v", err)
		return handler.ProgressEvent{
			OperationStatus:  handler.Failed,
			Message:          err.Error(),
			HandlerErrorCode: cloudformation.HandlerErrorCodeInvalidRequest}, nil
	}

	log.Debugf("Create - currentModel:%+v", currentModel)
	projectID := *currentModel.ProjectId
	container, err := validateOrCreateNetworkContainer(&req, prevModel, currentModel)

	if err != nil {
		log.Warnf("error network container mgmt: %v", err)
		return handler.ProgressEvent{
			OperationStatus:  handler.Failed,
			Message:          err.Error(),
			HandlerErrorCode: cloudformation.HandlerErrorCodeInvalidRequest}, nil
	}
	log.Debugf("Found valid container:%+v", container)

	peerRequest := mongodbatlas.Peer{
		ContainerID:  container.ID,
		VpcID:        *currentModel.VpcId,
		ProviderName: container.ProviderName,
	}

	region := currentModel.AccepterRegionName
	log.Debugf("Create region=%v ~~~~~~~~~~~~~~~~~~~~~~~~", *region)
	if region == nil || *region == "" {
		region = &req.RequestContext.Region
		log.Infof("AccepterRegionName was not set, default to req.RequestContext.Region:%v", region)
	}
	awsAccountId := currentModel.AwsAccountId
	if awsAccountId == nil || *awsAccountId == "" {
		awsAccountId = &req.RequestContext.AccountID
		log.Infof("AwsAccountIdwas not set, default to req.RequestContext.AccountID:%v", awsAccountId)
	}
	rtCIDR := currentModel.RouteTableCIDRBlock
	if rtCIDR == nil || *rtCIDR == "" {
		return handler.ProgressEvent{}, fmt.Errorf("error creating network peering: `RouteTableCIDRBlock` must be set")
	}

	peerRequest.AccepterRegionName = *region
	peerRequest.AWSAccountID = *awsAccountId
	peerRequest.RouteTableCIDRBlock = *rtCIDR
	log.Debugf("peerRequest:%+v", peerRequest)
	peerResponse, _, err := client.Peers.Create(context.Background(), projectID, &peerRequest)

	if err != nil {
		log.Warnf("error creating network peering: %s", err)
		return handler.ProgressEvent{
			OperationStatus:  handler.Failed,
			Message:          err.Error(),
			HandlerErrorCode: cloudformation.HandlerErrorCodeInvalidRequest}, nil
	}

	log.Debugf("Create peerResponse:%+v", peerResponse)
	currentModel.Id = &peerResponse.ID

	return handler.ProgressEvent{
		OperationStatus: handler.Success,
		Message:         "Create complete",
		ResourceModel:   currentModel,
	}, nil
}