Skip to content

Commit

Permalink
Support for client_certificate_id in `azurerm_api_management_backen…
Browse files Browse the repository at this point in the history
  • Loading branch information
yupwei68 authored and yupwei68 committed Jul 26, 2021
1 parent c97ca98 commit 875ad96
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,17 @@ func resourceApiManagementBackend() *pluginsdk.Resource {
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"client_certificate_id": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.CertificateID,
},

"client_certificate_thumbprint": {
Type: pluginsdk.TypeString,
Required: true,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"management_endpoints": {
Expand Down Expand Up @@ -469,14 +477,25 @@ func expandApiManagementBackendServiceFabricCluster(input []interface{}) (error,
return nil, nil
}
v := input[0].(map[string]interface{})
clientCertificatethumbprint := v["client_certificate_thumbprint"].(string)
managementEndpoints := v["management_endpoints"].(*pluginsdk.Set).List()
maxPartitionResolutionRetries := int32(v["max_partition_resolution_retries"].(int))
properties := apimanagement.BackendServiceFabricClusterProperties{
ClientCertificatethumbprint: utils.String(clientCertificatethumbprint),
ManagementEndpoints: utils.ExpandStringSlice(managementEndpoints),
MaxPartitionResolutionRetries: utils.Int32(maxPartitionResolutionRetries),
}

if v2, ok := v["client_certificate_thumbprint"].(string); ok && v2 != "" {
properties.ClientCertificatethumbprint = utils.String(v2)
}

if v2, ok := v["client_certificate_id"].(string); ok && v2 != "" {
properties.ClientCertificateID = utils.String(v2)
}

if properties.ClientCertificateID == nil && properties.ClientCertificatethumbprint == nil {
return fmt.Errorf("at least one of `client_certificate_thumbprint` and `client_certificate_id` must be set"), nil
}

serverCertificateThumbprintsUnset := true
serverX509NamesUnset := true
if serverCertificateThumbprints := v["server_certificate_thumbprints"]; serverCertificateThumbprints != nil {
Expand Down Expand Up @@ -589,6 +608,11 @@ func flattenApiManagementBackendServiceFabricCluster(input *apimanagement.Backen
if clientCertificatethumbprint := input.ClientCertificatethumbprint; clientCertificatethumbprint != nil {
result["client_certificate_thumbprint"] = *clientCertificatethumbprint
}

if input.ClientCertificateID != nil {
result["client_certificate_id"] = *input.ClientCertificateID
}

if managementEndpoints := input.ManagementEndpoints; managementEndpoints != nil {
result["management_endpoints"] = *managementEndpoints
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ func TestAccApiManagementBackend_serviceFabric(t *testing.T) {
})
}

func TestAccApiManagementBackend_serviceFabricClientCertificateId(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_backend", "test")
r := ApiManagementAuthorizationBackendResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.serviceFabricClientCertificateId(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccApiManagementBackend_disappears(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_backend", "test")
r := ApiManagementAuthorizationBackendResource{}
Expand Down Expand Up @@ -333,6 +348,39 @@ resource "azurerm_api_management_backend" "test" {
`, r.template(data, "sf"), data.RandomInteger)
}

func (r ApiManagementAuthorizationBackendResource) serviceFabricClientCertificateId(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_api_management_certificate" "test" {
name = "example-cert"
api_management_name = azurerm_api_management.test.name
resource_group_name = azurerm_resource_group.test.name
data = filebase64("testdata/keyvaultcert.pfx")
password = ""
}
resource "azurerm_api_management_backend" "test" {
name = "acctestapi-%d"
resource_group_name = azurerm_resource_group.test.name
api_management_name = azurerm_api_management.test.name
protocol = "http"
url = "fabric:/mytestapp/acctest"
service_fabric_cluster {
client_certificate_id = azurerm_api_management_certificate.test.id
management_endpoints = [
"https://acctestsf.com",
]
max_partition_resolution_retries = 5
server_certificate_thumbprints = [
azurerm_api_management_certificate.test.thumbprint,
azurerm_api_management_certificate.test.thumbprint,
]
}
}
`, r.template(data, "sf"), data.RandomInteger)
}

func (r ApiManagementAuthorizationBackendResource) requiresImport(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand Down Expand Up @@ -364,7 +412,7 @@ resource "azurerm_api_management" "test" {
resource_group_name = azurerm_resource_group.test.name
publisher_name = "pub1"
publisher_email = "[email protected]"
sku_name = "Developer_1"
sku_name = "Consumption_0"
}
`, data.RandomInteger, testName, data.Locations.Primary, data.RandomInteger, testName)
}
Expand Down
6 changes: 5 additions & 1 deletion website/docs/r/api_management_backend.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,12 @@ A `proxy` block supports the following:

A `service_fabric_cluster` block supports the following:

* `client_certificate_thumbprint` - (Required) The client certificate thumbprint for the management endpoint.
* `client_certificate_thumbprint` - (Optional) The client certificate thumbprint for the management endpoint.

* `client_certificate_id` - (Optional) The client certificate resource id for the management endpoint.

> **Note:** At least one of `client_certificate_thumbprint`, and `client_certificate_id` must be set.
>
* `management_endpoints` - (Required) A list of cluster management endpoints.

* `max_partition_resolution_retries` - (Required) The maximum number of retries when attempting resolve the partition.
Expand Down

0 comments on commit 875ad96

Please sign in to comment.