From 64105e83a9c23735bd85c247dd70b12e6b2aabc0 Mon Sep 17 00:00:00 2001 From: Victor Wernet Date: Wed, 28 Aug 2019 11:51:22 +0100 Subject: [PATCH] Added support for M2 and M5 (#52) --- pkg/broker/catalog.go | 71 ++++++++++++----- pkg/broker/instance_operations.go | 32 ++++---- test/integration/integration_test.go | 112 +++++++++++++++++++++++++++ 3 files changed, 181 insertions(+), 34 deletions(-) diff --git a/pkg/broker/catalog.go b/pkg/broker/catalog.go index 9c5b1dc..4ea389e 100644 --- a/pkg/broker/catalog.go +++ b/pkg/broker/catalog.go @@ -18,7 +18,33 @@ const idPrefix = "aosb-cluster" // providerNames contains all the available cloud providers on which clusters // may be provisioned. The available instance sizes for each provider are // fetched dynamically from the Atlas API. -var providerNames = []string{"AWS", "GCP", "AZURE"} +var ( + providerNames = []string{"AWS", "GCP", "AZURE", "TENANT"} + + // Hardcode the instance sizes for shared instances + sharedService = brokerapi.Service{ + ID: "aosb-cluster-service-tenant", + Name: "mongodb-atlas-tenant", + Description: "Atlas cluster hosted on \"TENANT\"", + Bindable: true, + InstancesRetrievable: false, + BindingsRetrievable: false, + Metadata: nil, + PlanUpdatable: true, + Plans: []brokerapi.ServicePlan{ + brokerapi.ServicePlan{ + ID: "aosb-cluster-plan-tenant-m2", + Name: "M2", + Description: "Instance size \"M2\"", + }, + brokerapi.ServicePlan{ + ID: "aosb-cluster-plan-tenant-m5", + Name: "M5", + Description: "Instance size \"M5\"", + }, + }, + } +) // Services generates the service catalog which will be presented to consumers of the API. func (b Broker) Services(ctx context.Context) ([]brokerapi.Service, error) { @@ -32,25 +58,30 @@ func (b Broker) Services(ctx context.Context) ([]brokerapi.Service, error) { } for i, providerName := range providerNames { - provider, err := client.GetProvider(providerName) - if err != nil { - return services, err - } - - // Create a CLI-friendly and user-friendly name. Will be displayed in the - // marketplace generated by the service catalog. - catalogName := fmt.Sprintf("mongodb-atlas-%s", strings.ToLower(provider.Name)) - - services[i] = brokerapi.Service{ - ID: serviceIDForProvider(provider), - Name: catalogName, - Description: fmt.Sprintf(`Atlas cluster hosted on "%s"`, provider.Name), - Bindable: true, - InstancesRetrievable: false, - BindingsRetrievable: false, - Metadata: nil, - PlanUpdatable: true, - Plans: plansForProvider(provider), + if providerName == "TENANT" { + services[i] = sharedService + } else { + + provider, err := client.GetProvider(providerName) + if err != nil { + return services, err + } + + // Create a CLI-friendly and user-friendly name. Will be displayed in the + // marketplace generated by the service catalog. + catalogName := fmt.Sprintf("mongodb-atlas-%s", strings.ToLower(provider.Name)) + + services[i] = brokerapi.Service{ + ID: serviceIDForProvider(provider), + Name: catalogName, + Description: fmt.Sprintf(`Atlas cluster hosted on "%s"`, provider.Name), + Bindable: true, + InstancesRetrievable: false, + BindingsRetrievable: false, + Metadata: nil, + PlanUpdatable: true, + Plans: plansForProvider(provider), + } } } diff --git a/pkg/broker/instance_operations.go b/pkg/broker/instance_operations.go index e1ef475..088b1cb 100644 --- a/pkg/broker/instance_operations.go +++ b/pkg/broker/instance_operations.go @@ -17,6 +17,8 @@ const ( OperationProvision = "provision" OperationDeprovision = "deprovision" OperationUpdate = "update" + InstanceSizeNameM2 = "M2" + InstanceSizeNameM5 = "M5" ) // Provision will create a new Atlas cluster with the instance ID as its name. @@ -249,27 +251,29 @@ func clusterFromParams(client atlas.Client, instanceID string, serviceID string, // If the plan ID is specified we construct the provider object from the service and plan. // The plan ID is optional during updates but not during creation. if planID != "" { - provider, err := findProviderByServiceID(client, serviceID) - if err != nil { - return nil, err - } - - instanceSize, err := findInstanceSizeByPlanID(provider, planID) - if err != nil { - return nil, err - } - if params.Cluster.ProviderSettings == nil { params.Cluster.ProviderSettings = &atlas.ProviderSettings{} } - // Configure provider based on service and plan. - params.Cluster.ProviderSettings.ProviderName = provider.Name - params.Cluster.ProviderSettings.InstanceSizeName = instanceSize.Name + instanceSizeName := params.Cluster.ProviderSettings.InstanceSizeName + if instanceSizeName != InstanceSizeNameM2 && instanceSizeName != InstanceSizeNameM5 { + provider, err := findProviderByServiceID(client, serviceID) + if err != nil { + return nil, err + } + + instanceSize, err := findInstanceSizeByPlanID(provider, planID) + if err != nil { + return nil, err + } + + // Configure provider based on service and plan. + params.Cluster.ProviderSettings.ProviderName = provider.Name + params.Cluster.ProviderSettings.InstanceSizeName = instanceSize.Name + } } // Add the instance ID as the name of the cluster. params.Cluster.Name = NormalizeClusterName(instanceID) - return params.Cluster, nil } diff --git a/test/integration/integration_test.go b/test/integration/integration_test.go index cf4f4e0..c2c28e2 100644 --- a/test/integration/integration_test.go +++ b/test/integration/integration_test.go @@ -142,6 +142,118 @@ func TestProvision(t *testing.T) { assert.Equal(t, expectedCluster, cluster) } +func TestProvisioM2Size(t *testing.T) { + t.Parallel() + + instanceID := uuid.New().String() + clusterName := brokerlib.NormalizeClusterName(instanceID) + + // Setting up our Expected cluster + var expectedCluster = &atlas.Cluster{ + Name: clusterName, + ProviderSettings: &atlas.ProviderSettings{ + BackingProviderName: "AWS", + InstanceSizeName: "M2", + ProviderName: "TENANT", + RegionName: "US_EAST_1", + }, + } + + // Setting up the params for the body request + paramsByte, marshalErr := json.Marshal(expectedCluster) + assert.NoError(t, marshalErr) + + params := `{"cluster":` + string(paramsByte) + `}` + + _, err := broker.Provision(ctx, instanceID, brokerapi.ProvisionDetails{ + ServiceID: "aosb-cluster-service-tenant", + PlanID: "aosb-cluster-plan-tenant-m2", + RawParameters: []byte(params), + }, true) + + defer teardownInstance(instanceID) + + if !assert.NoError(t, err) { + return + } + + // Ensure the cluster is being created. + cluster, err := client.GetCluster(clusterName) + assert.NoError(t, err) + assert.Equal(t, atlas.ClusterStateCreating, cluster.StateName) + + // Wait a maximum of 20 minutes for cluster to reach state idle. + err = waitForLastOperation(broker, instanceID, brokerlib.OperationProvision, 20) + if !assert.NoError(t, err) { + return + } + + cluster, err = client.GetCluster(clusterName) + assert.NoError(t, err) + + // Ensure response is equal to request cluster + assert.Equal(t, expectedCluster.ProviderSettings.BackingProviderName, cluster.ProviderSettings.BackingProviderName) + assert.Equal(t, expectedCluster.ProviderSettings.ProviderName, cluster.ProviderSettings.ProviderName) + assert.Equal(t, expectedCluster.ProviderSettings.InstanceSizeName, cluster.ProviderSettings.InstanceSizeName) + assert.Equal(t, expectedCluster.ProviderSettings.RegionName, cluster.ProviderSettings.RegionName) +} + +func TestProvisioM5Size(t *testing.T) { + t.Parallel() + + instanceID := uuid.New().String() + clusterName := brokerlib.NormalizeClusterName(instanceID) + + // Setting up our Expected cluster + var expectedCluster = &atlas.Cluster{ + Name: clusterName, + ProviderSettings: &atlas.ProviderSettings{ + BackingProviderName: "AWS", + InstanceSizeName: "M5", + ProviderName: "TENANT", + RegionName: "US_EAST_1", + }, + } + + // Setting up the params for the body request + paramsByte, marshalErr := json.Marshal(expectedCluster) + assert.NoError(t, marshalErr) + + params := `{"cluster":` + string(paramsByte) + `}` + + _, err := broker.Provision(ctx, instanceID, brokerapi.ProvisionDetails{ + ServiceID: "aosb-cluster-service-tenant", + PlanID: "aosb-cluster-plan-tenant-m5", + RawParameters: []byte(params), + }, true) + + defer teardownInstance(instanceID) + + if !assert.NoError(t, err) { + return + } + + // Ensure the cluster is being created. + cluster, err := client.GetCluster(clusterName) + assert.NoError(t, err) + assert.Equal(t, atlas.ClusterStateCreating, cluster.StateName) + + // Wait a maximum of 20 minutes for cluster to reach state idle. + err = waitForLastOperation(broker, instanceID, brokerlib.OperationProvision, 20) + if !assert.NoError(t, err) { + return + } + + cluster, err = client.GetCluster(clusterName) + assert.NoError(t, err) + + // Ensure response is equal to request cluster + assert.Equal(t, expectedCluster.ProviderSettings.BackingProviderName, cluster.ProviderSettings.BackingProviderName) + assert.Equal(t, expectedCluster.ProviderSettings.ProviderName, cluster.ProviderSettings.ProviderName) + assert.Equal(t, expectedCluster.ProviderSettings.InstanceSizeName, cluster.ProviderSettings.InstanceSizeName) + assert.Equal(t, expectedCluster.ProviderSettings.RegionName, cluster.ProviderSettings.RegionName) +} + func TestUpdate(t *testing.T) { t.Parallel()