Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 66 additions & 5 deletions app/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut,
n := &namespace.Namespace{
RequestId: ctx.String(RequestIDFlagName),
Namespace: ctx.String(NamespaceFlagName),
Spec: &namespace.NamespaceSpec{},
}

regions := ctx.StringSlice(namespaceRegionFlagName)
Expand All @@ -584,16 +585,56 @@ func NewNamespaceCommand(getNamespaceClientFn GetNamespaceClientFn) (CommandOut,
if len(regions) > 2 {
return fmt.Errorf("namespace can only be replicated up to 2 regions")
}
n.Spec = &namespace.NamespaceSpec{
PassiveRegions: regions[1:],

// Check if any region has cloud provider prefix
hasCloudPrefix := false
var regionIDs []string
for _, region := range regions {
if strings.HasPrefix(region, CloudProviderAWS) || strings.HasPrefix(region, CloudProviderGCP) {
hasCloudPrefix = true
regionIDs = append(regionIDs, region)
} else {
cloudProvider := ctx.String(cloudProviderFlagName)
if len(cloudProvider) == 0 {
return fmt.Errorf("namespace cloud provider is required when regions don't have cloud provider prefix")
}
regionIDs = append(regionIDs, fmt.Sprintf("%s-%s", cloudProvider, region))
break
Comment thread
yux0 marked this conversation as resolved.
Outdated
}
}

// If any region has cloud prefix, validate all regions have the same prefix
if hasCloudPrefix {
for _, region := range regions {
if !strings.HasPrefix(region, CloudProviderAWS) && !strings.HasPrefix(region, CloudProviderGCP) {
return fmt.Errorf("all regions must have the same cloud provider prefix. Found %s ", region)
}
}
}

cloudProvider := ctx.String(cloudProviderFlagName)
regionId, err := getRegionId(cloudProvider, regions[0])
// Set active region
activeRegionID := regionIDs[0]
activeRegion, err := regionIDFromString(activeRegionID)
if err != nil {
return err
}
n.Spec.RegionId = &regionId
n.Spec.RegionId = activeRegion

// Set passive regions if any
if len(regions) > 1 {
passiveRegions := make([]string, len(regionIDs)-1)
passiveRegionIDs := make([]*common.RegionID, len(regionIDs)-1)
for i, regionID := range regionIDs[1:] {
passiveRegions[i] = regionID
passiveRegionID, err := regionIDFromString(regionID)
if err != nil {
return err
}
passiveRegionIDs[i] = passiveRegionID
}
n.Spec.PassiveRegions = passiveRegions
Comment thread
yux0 marked this conversation as resolved.
Outdated
n.Spec.PassiveRegionIds = passiveRegionIDs
}

authMethod, err := toAuthMethod(ctx.String(authMethodFlagName))
if err != nil {
Expand Down Expand Up @@ -2207,6 +2248,26 @@ func getRegionId(cloudProvider, activeRegion string) (common.RegionID, error) {
return regionId, nil
}

// RegionIDFromString parses a region string and returns a RegionID. It must be in the format "<provider>-<region>".
func regionIDFromString(region string) (*common.RegionID, error) {
switch {
case strings.HasPrefix(region, CloudProviderAWS+"-"):
awsRegion := region[len(CloudProviderAWS)+1:]
return &common.RegionID{
Provider: common.CLOUD_PROVIDER_AWS,
Name: awsRegion,
}, nil
case strings.HasPrefix(region, CloudProviderGCP+"-"):
gcpRegion := region[len(CloudProviderGCP)+1:]
return &common.RegionID{
Provider: common.CLOUD_PROVIDER_GCP,
Name: gcpRegion,
}, nil
default:
return nil, fmt.Errorf("invalid region: %s", region)
}
}

func toAuthMethod(m string) (namespace.AuthMethod, error) {
switch m {
case AuthMethodRestricted:
Expand Down
Loading