Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
Added support for M2 and M5 (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgunior authored Aug 28, 2019
1 parent 41128ec commit 64105e8
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 34 deletions.
71 changes: 51 additions & 20 deletions pkg/broker/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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),
}
}
}

Expand Down
32 changes: 18 additions & 14 deletions pkg/broker/instance_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
112 changes: 112 additions & 0 deletions test/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 64105e8

Please sign in to comment.