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_api_management_certificate - allow key vault certificates #11175

Merged
merged 6 commits into from
May 1, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
keyVaultParse "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/keyvault/parse"
keyVaultValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/keyvault/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -43,16 +45,34 @@ func resourceApiManagementCertificate() *schema.Resource {
"api_management_name": schemaz.SchemaApiManagementName(),

"data": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validation.StringIsBase64,
AtLeastOneOf: []string{"data", "key_vault_secret_id"},
ConflictsWith: []string{"key_vault_secret_id", "key_vault_identity_client_id"},
},
MattiasAng marked this conversation as resolved.
Show resolved Hide resolved

"password": {
Type: schema.TypeString,
Required: true,
Optional: true,
Sensitive: true,
ValidateFunc: validation.StringIsBase64,
RequiredWith: []string{"data"},
},

"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
"key_vault_secret_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: keyVaultValidate.NestedItemIdWithOptionalVersion,
AtLeastOneOf: []string{"data", "key_vault_secret_id"},
ConflictsWith: []string{"data", "password"},
},

"key_vault_identity_client_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
RequiredWith: []string{"key_vault_secret_id"},
},

"expiration": {
Expand Down Expand Up @@ -83,6 +103,8 @@ func resourceApiManagementCertificateCreateUpdate(d *schema.ResourceData, meta i
serviceName := d.Get("api_management_name").(string)
data := d.Get("data").(string)
password := d.Get("password").(string)
keyVaultSecretId := d.Get("key_vault_secret_id").(string)
keyVaultIdentity := d.Get("key_vault_identity_client_id").(string)

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, serviceName, name)
Expand All @@ -98,10 +120,27 @@ func resourceApiManagementCertificateCreateUpdate(d *schema.ResourceData, meta i
}

parameters := apimanagement.CertificateCreateOrUpdateParameters{
CertificateCreateOrUpdateProperties: &apimanagement.CertificateCreateOrUpdateProperties{
Data: utils.String(data),
Password: utils.String(password),
},
CertificateCreateOrUpdateProperties: &apimanagement.CertificateCreateOrUpdateProperties{},
}

if keyVaultSecretId != "" {
parsedSecretId, err := keyVaultParse.ParseOptionallyVersionedNestedItemID(keyVaultSecretId)
if err != nil {
return err
}

parameters.KeyVault = &apimanagement.KeyVaultContractCreateProperties{
SecretIdentifier: utils.String(parsedSecretId.ID()),
}

if keyVaultIdentity != "" {
parameters.KeyVault.IdentityClientID = utils.String(keyVaultIdentity)
}
}

if data != "" {
parameters.Data = utils.String(data)
parameters.Password = utils.String(password)
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, name, parameters, ""); err != nil {
Expand All @@ -113,7 +152,7 @@ func resourceApiManagementCertificateCreateUpdate(d *schema.ResourceData, meta i
return fmt.Errorf("retrieving Certificate %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err)
}
if resp.ID == nil {
return fmt.Errorf("Cannot read ID for Certificate %q (Resource Group %q / API Management Service %q)", name, resourceGroup, serviceName)
return fmt.Errorf("cannot read ID for Certificate %q (Resource Group %q / API Management Service %q)", name, resourceGroup, serviceName)
}
d.SetId(*resp.ID)

Expand Down Expand Up @@ -153,9 +192,13 @@ func resourceApiManagementCertificateRead(d *schema.ResourceData, meta interface
formatted := expiration.Format(time.RFC3339)
d.Set("expiration", formatted)
}

d.Set("subject", props.Thumbprint)
d.Set("thumbprint", props.Thumbprint)

if keyvault := props.KeyVault; keyvault != nil {
d.Set("key_vault_secret_id", keyvault.SecretIdentifier)
d.Set("key_vault_identity_client_id", keyvault.IdentityClientID)
}
}

return nil
Expand Down
Loading