Skip to content

Commit

Permalink
new resources: azurerm_cosmosdb_cassandra_datacenter, azurerm_cosmosd…
Browse files Browse the repository at this point in the history
…b_cassandra_cluster (#14019)

Co-authored-by: Theo van Kraay <thvankra@microsoft.com>
  • Loading branch information
TheovanKraay and Theo van Kraay authored Nov 5, 2021
1 parent ed2701d commit f68aa17
Show file tree
Hide file tree
Showing 88 changed files with 4,285 additions and 210 deletions.
44 changes: 27 additions & 17 deletions internal/services/cosmos/client/client.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
CassandraClient *documentdb.CassandraResourcesClient
DatabaseClient *documentdb.DatabaseAccountsClient
GremlinClient *documentdb.GremlinResourcesClient
MongoDbClient *documentdb.MongoDBResourcesClient
NotebookWorkspaceClient *documentdb.NotebookWorkspacesClient
SqlClient *documentdb.SQLResourcesClient
SqlResourceClient *documentdb.SQLResourcesClient
TableClient *documentdb.TableResourcesClient
CassandraClient *documentdb.CassandraResourcesClient
CassandraClustersClient *documentdb.CassandraClustersClient
CassandraDatacentersClient *documentdb.CassandraDataCentersClient
DatabaseClient *documentdb.DatabaseAccountsClient
GremlinClient *documentdb.GremlinResourcesClient
MongoDbClient *documentdb.MongoDBResourcesClient
NotebookWorkspaceClient *documentdb.NotebookWorkspacesClient
SqlClient *documentdb.SQLResourcesClient
SqlResourceClient *documentdb.SQLResourcesClient
TableClient *documentdb.TableResourcesClient
}

func NewClient(o *common.ClientOptions) *Client {
cassandraClient := documentdb.NewCassandraResourcesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&cassandraClient.Client, o.ResourceManagerAuthorizer)

cassandraClustersClient := documentdb.NewCassandraClustersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&cassandraClustersClient.Client, o.ResourceManagerAuthorizer)

cassandraDatacentersClient := documentdb.NewCassandraDataCentersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&cassandraDatacentersClient.Client, o.ResourceManagerAuthorizer)

databaseClient := documentdb.NewDatabaseAccountsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&databaseClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -42,13 +50,15 @@ func NewClient(o *common.ClientOptions) *Client {
o.ConfigureClient(&tableClient.Client, o.ResourceManagerAuthorizer)

return &Client{
CassandraClient: &cassandraClient,
DatabaseClient: &databaseClient,
GremlinClient: &gremlinClient,
MongoDbClient: &mongoDbClient,
NotebookWorkspaceClient: &notebookWorkspaceClient,
SqlClient: &sqlClient,
SqlResourceClient: &sqlResourceClient,
TableClient: &tableClient,
CassandraClient: &cassandraClient,
CassandraClustersClient: &cassandraClustersClient,
CassandraDatacentersClient: &cassandraDatacentersClient,
DatabaseClient: &databaseClient,
GremlinClient: &gremlinClient,
MongoDbClient: &mongoDbClient,
NotebookWorkspaceClient: &notebookWorkspaceClient,
SqlClient: &sqlClient,
SqlResourceClient: &sqlResourceClient,
TableClient: &tableClient,
}
}
2 changes: 1 addition & 1 deletion internal/services/cosmos/common/autoscale_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"log"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package common

import (
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/common/cors_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"strings"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/utils"
Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/common/indexing_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/common/indexing_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"testing"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/common/ip_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"strings"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/common/ip_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"testing"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/common/schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package common

import (
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/suppress"
Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/common/throughput.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package common
import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/cosmosdb_account_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sort"
"time"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/common"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/cosmosdb_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/cosmosdb_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"testing"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-06-15/documentdb"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
Expand Down
168 changes: 168 additions & 0 deletions internal/services/cosmos/cosmosdb_cassandra_cluster_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package cosmos

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/go-azure-helpers/response"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/parse"
networkValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceCassandraCluster() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceCassandraClusterCreate,
Read: resourceCassandraClusterRead,
Delete: resourceCassandraClusterDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.CassandraClusterID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"resource_group_name": azure.SchemaResourceGroupName(),

"location": location.Schema(),

"delegated_management_subnet_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: networkValidate.SubnetID,
},

"default_admin_password": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
}
}

func resourceCassandraClusterCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Cosmos.CassandraClustersClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

resourceGroupName := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
id := parse.NewCassandraClusterID(subscriptionId, resourceGroupName, name)

existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_cosmosdb_cassandra_cluster", id.ID())
}

body := documentdb.ClusterResource{
Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))),
Properties: &documentdb.ClusterResourceProperties{
DelegatedManagementSubnetID: utils.String(d.Get("delegated_management_subnet_id").(string)),
InitialCassandraAdminPassword: utils.String(d.Get("default_admin_password").(string)),
},
}

future, err := client.CreateUpdate(ctx, id.ResourceGroup, id.Name, body)
if err != nil {
return fmt.Errorf("creating %q: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting on create/update for %q: %+v", id, err)
}

d.SetId(id.ID())

return resourceCassandraClusterRead(d, meta)
}

func resourceCassandraClusterRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Cosmos.CassandraClustersClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.CassandraClusterID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Error reading %q - removing from state", id)
d.SetId("")
return nil
}

return fmt.Errorf("reading %q: %+v", id, err)
}

d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("name", id.Name)
if props := resp.Properties; props != nil {
if res := props; res != nil {
d.Set("delegated_management_subnet_id", props.DelegatedManagementSubnetID)
}
}
// The "default_admin_password" is not returned in GET response, hence setting it from config.
d.Set("default_admin_password", d.Get("default_admin_password").(string))
return nil
}

func resourceCassandraClusterDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Cosmos.CassandraClustersClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.CassandraClusterID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf("deleting %q: %+v", id, err)
}
}

err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return fmt.Errorf("waiting on delete future for %q: %+v", id, err)
}

return nil
}
Loading

0 comments on commit f68aa17

Please sign in to comment.