Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_cosmosdb_cassandra_cluster - support new properties #16799

Merged
merged 12 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
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
235 changes: 234 additions & 1 deletion internal/services/cosmos/cosmosdb_cassandra_cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"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/services/attestation/validate"
"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/tags"
Expand Down Expand Up @@ -68,6 +70,77 @@ func resourceCassandraCluster() *pluginsdk.Resource {
ValidateFunc: validation.StringIsNotEmpty,
},

"authentication_method": {
Type: pluginsdk.TypeString,
Optional: true,
Default: string(documentdb.AuthenticationMethodCassandra),
ValidateFunc: validation.StringInSlice([]string{
string(documentdb.AuthenticationMethodNone),
string(documentdb.AuthenticationMethodCassandra),
}, false),
},

"client_certificate": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"pem": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.IsCert,
},
},
},
},

"external_gossip_certificate": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"pem": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.IsCert,
},
},
},
},

"external_seed_node": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"ip_address": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.IsIPv4Address,
},
},
},
},

"identity": commonschema.SystemAssignedIdentityOptional(),

"repair_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"version": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "3.11",
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"3.11",
"4.0",
}, false),
},

"tags": tags.Schema(),
},
}
Expand All @@ -93,15 +166,36 @@ func resourceCassandraClusterCreate(d *pluginsdk.ResourceData, meta interface{})
return tf.ImportAsExistsError("azurerm_cosmosdb_cassandra_cluster", id.ID())
}

expandedIdentity, err := expandCassandraClusterIdentity(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}

body := documentdb.ClusterResource{
Identity: expandedIdentity,
Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))),
Properties: &documentdb.ClusterResourceProperties{
AuthenticationMethod: documentdb.AuthenticationMethod(d.Get("authentication_method").(string)),
CassandraVersion: utils.String(d.Get("version").(string)),
DelegatedManagementSubnetID: utils.String(d.Get("delegated_management_subnet_id").(string)),
InitialCassandraAdminPassword: utils.String(d.Get("default_admin_password").(string)),
RepairEnabled: utils.Bool(d.Get("repair_enabled").(bool)),
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if v, ok := d.GetOk("client_certificate"); ok {
body.Properties.ClientCertificates = expandCassandraClusterCertificate(v.([]interface{}))
}

if v, ok := d.GetOk("external_gossip_certificate"); ok {
body.Properties.ExternalGossipCertificates = expandCassandraClusterCertificate(v.([]interface{}))
}

if v, ok := d.GetOk("external_seed_node"); ok {
body.Properties.ExternalSeedNodes = expandCassandraClusterExternalSeedNode(v.([]interface{}))
}

future, err := client.CreateUpdate(ctx, id.ResourceGroup, id.Name, body)
if err != nil {
return fmt.Errorf("creating %q: %+v", id, err)
Expand Down Expand Up @@ -143,8 +237,30 @@ func resourceCassandraClusterRead(d *pluginsdk.ResourceData, meta interface{}) e
if props := resp.Properties; props != nil {
if res := props; res != nil {
d.Set("delegated_management_subnet_id", props.DelegatedManagementSubnetID)
d.Set("authentication_method", string(props.AuthenticationMethod))
d.Set("repair_enabled", props.RepairEnabled)
d.Set("version", props.CassandraVersion)

if err := d.Set("client_certificate", flattenCassandraClusterCertificate(props.ClientCertificates)); err != nil {
return fmt.Errorf("setting `client_certificate`: %+v", err)
}

if err := d.Set("external_gossip_certificate", flattenCassandraClusterCertificate(props.ExternalGossipCertificates)); err != nil {
return fmt.Errorf("setting `external_gossip_certificate`: %+v", err)
}

if err := d.Set("external_seed_node", flattenCassandraClusterExternalSeedNode(props.ExternalSeedNodes)); err != nil {
return fmt.Errorf("setting `external_seed_node`: %+v", err)
}
}
}

if v := resp.Identity; v != nil {
if err := d.Set("identity", flattenCassandraClusterIdentity(v)); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}
}

// 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 tags.FlattenAndSet(d, resp.Tags)
Expand All @@ -160,17 +276,38 @@ func resourceCassandraClusterUpdate(d *pluginsdk.ResourceData, meta interface{})
name := d.Get("name").(string)
id := parse.NewCassandraClusterID(subscriptionId, resourceGroupName, name)

expandedIdentity, err := expandCassandraClusterIdentity(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}

body := documentdb.ClusterResource{
Identity: expandedIdentity,
Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))),
Properties: &documentdb.ClusterResourceProperties{
AuthenticationMethod: documentdb.AuthenticationMethod(d.Get("authentication_method").(string)),
CassandraVersion: utils.String(d.Get("version").(string)),
DelegatedManagementSubnetID: utils.String(d.Get("delegated_management_subnet_id").(string)),
InitialCassandraAdminPassword: utils.String(d.Get("default_admin_password").(string)),
RepairEnabled: utils.Bool(d.Get("repair_enabled").(bool)),
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if v, ok := d.GetOk("client_certificate"); ok {
body.Properties.ClientCertificates = expandCassandraClusterCertificate(v.([]interface{}))
}

if v, ok := d.GetOk("external_gossip_certificate"); ok {
body.Properties.ExternalGossipCertificates = expandCassandraClusterCertificate(v.([]interface{}))
}

if v, ok := d.GetOk("external_seed_node"); ok {
body.Properties.ExternalSeedNodes = expandCassandraClusterExternalSeedNode(v.([]interface{}))
}

// Though there is update method but Service API complains it isn't implemented
_, err := client.CreateUpdate(ctx, id.ResourceGroup, id.Name, body)
_, err = client.CreateUpdate(ctx, id.ResourceGroup, id.Name, body)
if err != nil {
return fmt.Errorf("updating %q: %+v", id, err)
}
Expand Down Expand Up @@ -232,5 +369,101 @@ func cosmosdbCassandraClusterStateRefreshFunc(ctx context.Context, client *docum
}
return nil, "", fmt.Errorf("unable to read provisioning state")
}
}

func expandCassandraClusterIdentity(input []interface{}) (*documentdb.ManagedCassandraManagedServiceIdentity, error) {
expanded, err := identity.ExpandSystemAssigned(input)
if err != nil {
return nil, err
}

return &documentdb.ManagedCassandraManagedServiceIdentity{
Type: documentdb.ManagedCassandraResourceIdentityType(string(expanded.Type)),
}, nil
}

func expandCassandraClusterCertificate(input []interface{}) *[]documentdb.Certificate {
results := make([]documentdb.Certificate, 0)
for _, seedNode := range input {
v := seedNode.(map[string]interface{})

result := documentdb.Certificate{
Pem: utils.String(v["pem"].(string)),
}
results = append(results, result)
}

return &results
}

func expandCassandraClusterExternalSeedNode(input []interface{}) *[]documentdb.SeedNode {
results := make([]documentdb.SeedNode, 0)
for _, seedNode := range input {
v := seedNode.(map[string]interface{})

result := documentdb.SeedNode{
IPAddress: utils.String(v["ip_address"].(string)),
}
results = append(results, result)
}

return &results
}

func flattenCassandraClusterCertificate(input *[]documentdb.Certificate) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

for _, v := range *input {
var pem string
if v.Pem != nil {
pem = *v.Pem
}

results = append(results, map[string]interface{}{
"pem": pem,
})
}

return results
}

func flattenCassandraClusterExternalSeedNode(input *[]documentdb.SeedNode) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

for _, v := range *input {
var ipAddress string
if v.IPAddress != nil {
ipAddress = *v.IPAddress
}

results = append(results, map[string]interface{}{
"ip_address": ipAddress,
})
}

return results
}

func flattenCassandraClusterIdentity(input *documentdb.ManagedCassandraManagedServiceIdentity) []interface{} {
var transform *identity.SystemAssigned

if input != nil {
transform = &identity.SystemAssigned{
Type: identity.Type(string(input.Type)),
}
if input.PrincipalID != nil {
transform.PrincipalId = *input.PrincipalID
}
if input.TenantID != nil {
transform.TenantId = *input.TenantID
}
}

return identity.FlattenSystemAssigned(transform)
}
Loading