in pkg/deploy/lattice/service_network_manager.go [172:224]
func (m *defaultServiceNetworkManager) CreateOrUpdate(ctx context.Context, serviceNetwork *model.ServiceNetwork) (model.ServiceNetworkStatus, error) {
// check if exists
foundSnSummary, err := m.cloud.Lattice().FindServiceNetwork(ctx, serviceNetwork.Spec.Name)
if err != nil && !services.IsNotFoundError(err) {
return model.ServiceNetworkStatus{ServiceNetworkARN: "", ServiceNetworkID: ""}, err
}
var serviceNetworkId string
var serviceNetworkArn string
vpcLatticeSess := m.cloud.Lattice()
if foundSnSummary == nil {
m.log.Debugf(ctx, "Creating ServiceNetwork %s and tagging it with vpcId %s",
serviceNetwork.Spec.Name, config.VpcID)
serviceNetworkInput := vpclattice.CreateServiceNetworkInput{
Name: &serviceNetwork.Spec.Name,
Tags: m.cloud.DefaultTags(),
}
resp, err := vpcLatticeSess.CreateServiceNetworkWithContext(ctx, &serviceNetworkInput)
if err != nil {
return model.ServiceNetworkStatus{}, err
}
serviceNetworkId = aws.StringValue(resp.Id)
serviceNetworkArn = aws.StringValue(resp.Arn)
} else {
m.log.Debugf(ctx, "ServiceNetwork %s exists, checking its VPC association", serviceNetwork.Spec.Name)
serviceNetworkId = aws.StringValue(foundSnSummary.SvcNetwork.Id)
serviceNetworkArn = aws.StringValue(foundSnSummary.SvcNetwork.Arn)
snva, err := m.getActiveVpcAssociation(ctx, serviceNetworkId)
if err != nil {
return model.ServiceNetworkStatus{}, err
}
if snva != nil {
m.log.Debugf(ctx, "ServiceNetwork %s already has VPC association %s",
serviceNetwork.Spec.Name, aws.StringValue(snva.Arn))
return model.ServiceNetworkStatus{ServiceNetworkARN: serviceNetworkArn, ServiceNetworkID: serviceNetworkId}, nil
}
}
m.log.Debugf(ctx, "Creating association between ServiceNetwork %s and VPC %s", serviceNetworkId, config.VpcID)
createServiceNetworkVpcAssociationInput := vpclattice.CreateServiceNetworkVpcAssociationInput{
ServiceNetworkIdentifier: &serviceNetworkId,
VpcIdentifier: &config.VpcID,
Tags: m.cloud.DefaultTags(),
}
_, err = vpcLatticeSess.CreateServiceNetworkVpcAssociationWithContext(ctx, &createServiceNetworkVpcAssociationInput)
if err != nil {
return model.ServiceNetworkStatus{}, err
}
return model.ServiceNetworkStatus{ServiceNetworkARN: serviceNetworkArn, ServiceNetworkID: serviceNetworkId}, nil
}