diff --git a/azurerm/internal/services/iothub/registration.go b/azurerm/internal/services/iothub/registration.go index 5bdcfdd49312..62c25ce49a2f 100644 --- a/azurerm/internal/services/iothub/registration.go +++ b/azurerm/internal/services/iothub/registration.go @@ -23,8 +23,6 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource { // SupportedResources returns the supported Resources supported by this Service func (r Registration) SupportedResources() map[string]*schema.Resource { return map[string]*schema.Resource{ - "azurerm_iot_dps": resourceArmIotDPS(), - "azurerm_iot_dps_certificate": resourceArmIotDPSCertificate(), "azurerm_iothub_dps": resourceArmIotHubDPS(), "azurerm_iothub_dps_certificate": resourceArmIotHubDPSCertificate(), "azurerm_iothub_dps_shared_access_policy": resourceArmIotHubDPSSharedAccessPolicy(), diff --git a/azurerm/internal/services/iothub/resource_arm_iot_dps.go b/azurerm/internal/services/iothub/resource_arm_iot_dps.go deleted file mode 100644 index 31818c1d88e8..000000000000 --- a/azurerm/internal/services/iothub/resource_arm_iot_dps.go +++ /dev/null @@ -1,381 +0,0 @@ -package iothub - -import ( - "context" - "fmt" - "log" - "regexp" - "strconv" - "time" - - "github.com/Azure/azure-sdk-for-go/services/preview/iothub/mgmt/2018-12-01-preview/devices" - "github.com/Azure/azure-sdk-for-go/services/provisioningservices/mgmt/2018-01-22/iothub" - "github.com/hashicorp/go-azure-helpers/response" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" -) - -func resourceArmIotDPS() *schema.Resource { - return &schema.Resource{ - Create: resourceArmIotDPSCreateUpdate, - Read: resourceArmIotDPSRead, - Update: resourceArmIotDPSCreateUpdate, - Delete: resourceArmIotDPSDelete, - - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, - DeprecationMessage: `The 'azurerm_iot_dps' resource is deprecated in favour of the renamed version 'azurerm_iothub_dps'. - -Information on migrating to the renamed resource can be found here: https://terraform.io/docs/providers/azurerm/guides/migrating-between-renamed-resources.html - -As such the existing 'azurerm_iot_dps' resource is deprecated and will be removed in the next major version of the AzureRM Provider (2.0). -`, - - Timeouts: &schema.ResourceTimeout{ - Create: schema.DefaultTimeout(30 * time.Minute), - Read: schema.DefaultTimeout(5 * time.Minute), - Update: schema.DefaultTimeout(30 * time.Minute), - Delete: schema.DefaultTimeout(30 * time.Minute), - }, - - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validate.IoTHubName, - }, - - "resource_group_name": azure.SchemaResourceGroupName(), // azure.SchemaResourceGroupNameDiffSuppress(), - - "location": azure.SchemaLocation(), - - "sku": { - Type: schema.TypeList, - MaxItems: 1, - Required: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - DiffSuppressFunc: suppress.CaseDifference, - ValidateFunc: validation.StringInSlice([]string{ - string(devices.B1), - string(devices.B2), - string(devices.B3), - string(devices.F1), - string(devices.S1), - string(devices.S2), - string(devices.S3), - }, true), - }, - - "tier": { - Type: schema.TypeString, - Optional: true, - Computed: true, - Deprecated: "This property is no longer required and will be removed in version 2.0 of the provider", - DiffSuppressFunc: suppress.CaseDifference, - ValidateFunc: validation.StringInSlice([]string{ - string(devices.Basic), - string(devices.Free), - string(devices.Standard), - }, true), - }, - - "capacity": { - Type: schema.TypeInt, - Required: true, - ValidateFunc: validation.IntAtLeast(1), - }, - }, - }, - }, - - "linked_hub": { - Type: schema.TypeList, - Optional: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "connection_string": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringIsNotEmpty, - ForceNew: true, - // Azure returns the key as ****. We'll suppress that here. - DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { - secretKeyRegex := regexp.MustCompile("(SharedAccessKey)=[^;]+") - maskedNew := secretKeyRegex.ReplaceAllString(new, "$1=****") - return (new == d.Get(k).(string)) && (maskedNew == old) - }, - Sensitive: true, - }, - "location": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringIsNotEmpty, - StateFunc: azure.NormalizeLocation, - ForceNew: true, - }, - "apply_allocation_policy": { - Type: schema.TypeBool, - Optional: true, - Default: false, - }, - "allocation_weight": { - Type: schema.TypeInt, - Optional: true, - Default: 0, - ValidateFunc: validation.IntBetween(0, 1000), - }, - "hostname": { - Type: schema.TypeString, - Computed: true, - }, - }, - }, - }, - - "tags": tags.Schema(), - }, - } -} - -func resourceArmIotDPSCreateUpdate(d *schema.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).IoTHub.DPSResourceClient - ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) - defer cancel() - - name := d.Get("name").(string) - resourceGroup := d.Get("resource_group_name").(string) - - if features.ShouldResourcesBeImported() && d.IsNewResource() { - existing, err := client.Get(ctx, name, resourceGroup) - if err != nil { - if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("Error checking for presence of existing IoT Device Provisioning Service %q (Resource Group %q): %+v", name, resourceGroup, err) - } - } - - if existing.ID != nil && *existing.ID != "" { - return tf.ImportAsExistsError("azurerm_iot_dps", *existing.ID) - } - } - - iotdps := iothub.ProvisioningServiceDescription{ - Location: utils.String(d.Get("location").(string)), - Name: utils.String(name), - Sku: expandIoTDPSSku(d), - Properties: &iothub.IotDpsPropertiesDescription{ - IotHubs: expandIoTDPSIoTHubs(d.Get("linked_hub").([]interface{})), - }, - Tags: tags.Expand(d.Get("tags").(map[string]interface{})), - } - - future, err := client.CreateOrUpdate(ctx, resourceGroup, name, iotdps) - if err != nil { - return fmt.Errorf("Error creating/updating IoT Device Provisioning Service %q (Resource Group %q): %+v", name, resourceGroup, err) - } - - if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("Error waiting for the completion of the creating/updating of IoT Device Provisioning Service %q (Resource Group %q): %+v", name, resourceGroup, err) - } - - resp, err := client.Get(ctx, name, resourceGroup) - if err != nil { - return fmt.Errorf("Error retrieving IoT Device Provisioning Service %q (Resource Group %q): %+v", name, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read IoT Device Provisioning Service %q (Resource Group %q): %+v", name, resourceGroup, err) - } - - d.SetId(*resp.ID) - - return resourceArmIotDPSRead(d, meta) -} - -func resourceArmIotDPSRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).IoTHub.DPSResourceClient - ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) - defer cancel() - - id, err := azure.ParseAzureResourceID(d.Id()) - if err != nil { - return err - } - resourceGroup := id.ResourceGroup - name := id.Path["provisioningServices"] - - resp, err := client.Get(ctx, name, resourceGroup) - if err != nil { - if utils.ResponseWasNotFound(resp.Response) { - d.SetId("") - return nil - } - - return fmt.Errorf("Error retrieving IoT Device Provisioning Service %q (Resource Group %q): %+v", name, resourceGroup, err) - } - - d.Set("name", resp.Name) - d.Set("resource_group_name", resourceGroup) - if location := resp.Location; location != nil { - d.Set("location", azure.NormalizeLocation(*location)) - } - sku := flattenIoTDPSSku(resp.Sku) - if err := d.Set("sku", sku); err != nil { - return fmt.Errorf("Error setting `sku`: %+v", err) - } - - if props := resp.Properties; props != nil { - if err := d.Set("linked_hub", flattenIoTDPSLinkedHub(props.IotHubs)); err != nil { - return fmt.Errorf("Error setting `linked_hub`: %+v", err) - } - } - - return tags.FlattenAndSet(d, resp.Tags) -} - -func resourceArmIotDPSDelete(d *schema.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).IoTHub.DPSResourceClient - ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) - defer cancel() - - id, err := azure.ParseAzureResourceID(d.Id()) - if err != nil { - return err - } - resourceGroup := id.ResourceGroup - name := id.Path["provisioningServices"] - - future, err := client.Delete(ctx, name, resourceGroup) - if err != nil { - if !response.WasNotFound(future.Response()) { - return fmt.Errorf("Error deleting IoT Device Provisioning Service %q (Resource Group %q): %+v", name, resourceGroup, err) - } - } - - return waitForIotDPSToBeDeleted(ctx, client, resourceGroup, name, d) -} - -func waitForIotDPSToBeDeleted(ctx context.Context, client *iothub.IotDpsResourceClient, resourceGroup, name string, d *schema.ResourceData) error { - // we can't use the Waiter here since the API returns a 404 once it's deleted which is considered a polling status code.. - log.Printf("[DEBUG] Waiting for IoT Device Provisioning Service %q (Resource Group %q) to be deleted", name, resourceGroup) - stateConf := &resource.StateChangeConf{ - Pending: []string{"200"}, - Target: []string{"404"}, - Refresh: iotdpsStateStatusCodeRefreshFunc(ctx, client, resourceGroup, name), - Timeout: d.Timeout(schema.TimeoutDelete), - } - - if _, err := stateConf.WaitForState(); err != nil { - return fmt.Errorf("Error waiting for IoT Device Provisioning Service %q (Resource Group %q) to be deleted: %+v", name, resourceGroup, err) - } - - return nil -} - -func iotdpsStateStatusCodeRefreshFunc(ctx context.Context, client *iothub.IotDpsResourceClient, resourceGroup, name string) resource.StateRefreshFunc { - return func() (interface{}, string, error) { - res, err := client.Get(ctx, name, resourceGroup) - - log.Printf("Retrieving IoT Device Provisioning Service %q (Resource Group %q) returned Status %d", resourceGroup, name, res.StatusCode) - - if err != nil { - if utils.ResponseWasNotFound(res.Response) { - return res, strconv.Itoa(res.StatusCode), nil - } - return nil, "", fmt.Errorf("Error polling for the status of the IoT Device Provisioning Service %q (Resource Group %q): %+v", name, resourceGroup, err) - } - - return res, strconv.Itoa(res.StatusCode), nil - } -} - -func expandIoTDPSSku(d *schema.ResourceData) *iothub.IotDpsSkuInfo { - skuList := d.Get("sku").([]interface{}) - skuMap := skuList[0].(map[string]interface{}) - capacity := int64(skuMap["capacity"].(int)) - - name := skuMap["name"].(string) - tier := skuMap["tier"].(string) - - return &iothub.IotDpsSkuInfo{ - Name: iothub.IotDpsSku(name), - Tier: utils.String(tier), - Capacity: utils.Int64(capacity), - } -} - -func expandIoTDPSIoTHubs(input []interface{}) *[]iothub.DefinitionDescription { - linkedHubs := make([]iothub.DefinitionDescription, 0) - - for _, attr := range input { - linkedHubConfig := attr.(map[string]interface{}) - linkedHub := iothub.DefinitionDescription{ - ConnectionString: utils.String(linkedHubConfig["connection_string"].(string)), - AllocationWeight: utils.Int32(int32(linkedHubConfig["allocation_weight"].(int))), - ApplyAllocationPolicy: utils.Bool(linkedHubConfig["apply_allocation_policy"].(bool)), - Location: utils.String(linkedHubConfig["location"].(string)), - } - - linkedHubs = append(linkedHubs, linkedHub) - } - - return &linkedHubs -} - -func flattenIoTDPSSku(input *iothub.IotDpsSkuInfo) []interface{} { - output := make(map[string]interface{}) - - output["name"] = string(input.Name) - output["tier"] = input.Tier - if capacity := input.Capacity; capacity != nil { - output["capacity"] = int(*capacity) - } - - return []interface{}{output} -} - -func flattenIoTDPSLinkedHub(input *[]iothub.DefinitionDescription) []interface{} { - linkedHubs := make([]interface{}, 0) - if input == nil { - return linkedHubs - } - - for _, attr := range *input { - linkedHub := make(map[string]interface{}) - - if attr.Name != nil { - linkedHub["hostname"] = *attr.Name - } - if attr.ApplyAllocationPolicy != nil { - linkedHub["apply_allocation_policy"] = *attr.ApplyAllocationPolicy - } - if attr.AllocationWeight != nil { - linkedHub["allocation_weight"] = *attr.AllocationWeight - } - if attr.ConnectionString != nil { - linkedHub["connection_string"] = *attr.ConnectionString - } - if attr.Location != nil { - linkedHub["location"] = *attr.Location - } - - linkedHubs = append(linkedHubs, linkedHub) - } - - return linkedHubs -} diff --git a/azurerm/internal/services/iothub/resource_arm_iot_dps_certificate.go b/azurerm/internal/services/iothub/resource_arm_iot_dps_certificate.go deleted file mode 100644 index 694cbf1a8fe1..000000000000 --- a/azurerm/internal/services/iothub/resource_arm_iot_dps_certificate.go +++ /dev/null @@ -1,174 +0,0 @@ -package iothub - -import ( - "fmt" - "time" - - "github.com/Azure/azure-sdk-for-go/services/provisioningservices/mgmt/2018-01-22/iothub" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" - "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/helpers/validate" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" -) - -func resourceArmIotDPSCertificate() *schema.Resource { - return &schema.Resource{ - Create: resourceArmIotDPSCertificateCreateUpdate, - Read: resourceArmIotDPSCertificateRead, - Update: resourceArmIotDPSCertificateCreateUpdate, - Delete: resourceArmIotDPSCertificateDelete, - - Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, - }, - DeprecationMessage: `The 'azurerm_iot_dps_certificate' resource is deprecated in favour of the renamed version 'azurerm_iothub_dps_certificate'. - -Information on migrating to the renamed resource can be found here: https://terraform.io/docs/providers/azurerm/guides/migrating-between-renamed-resources.html - -As such the existing 'azurerm_iot_dps_certificate' resource is deprecated and will be removed in the next major version of the AzureRM Provider (2.0). -`, - - Timeouts: &schema.ResourceTimeout{ - Create: schema.DefaultTimeout(30 * time.Minute), - Read: schema.DefaultTimeout(5 * time.Minute), - Update: schema.DefaultTimeout(30 * time.Minute), - Delete: schema.DefaultTimeout(30 * time.Minute), - }, - - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validate.IoTHubName, - }, - - "resource_group_name": azure.SchemaResourceGroupName(), - - "iot_dps_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validate.IoTHubName, - }, - - "certificate_content": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringIsNotEmpty, - Sensitive: true, - }, - }, - } -} - -func resourceArmIotDPSCertificateCreateUpdate(d *schema.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).IoTHub.DPSCertificateClient - ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) - defer cancel() - - name := d.Get("name").(string) - resourceGroup := d.Get("resource_group_name").(string) - iotDPSName := d.Get("iot_dps_name").(string) - - if features.ShouldResourcesBeImported() && d.IsNewResource() { - existing, err := client.Get(ctx, name, resourceGroup, iotDPSName, "") - if err != nil { - if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("Error checking for presence of existing IoT Device Provisioning Service Certificate %q (Device Provisioning Service %q / Resource Group %q): %+v", name, iotDPSName, resourceGroup, err) - } - } - - if existing.ID != nil && *existing.ID != "" { - return tf.ImportAsExistsError("azurerm_iot_dps_certificate", *existing.ID) - } - } - - certificate := iothub.CertificateBodyDescription{ - Certificate: utils.String(d.Get("certificate_content").(string)), - } - - if _, err := client.CreateOrUpdate(ctx, resourceGroup, iotDPSName, name, certificate, ""); err != nil { - return fmt.Errorf("Error creating/updating IoT Device Provisioning Service Certificate %q (Device Provisioning Service %q / Resource Group %q): %+v", name, iotDPSName, resourceGroup, err) - } - - resp, err := client.Get(ctx, name, resourceGroup, iotDPSName, "") - if err != nil { - return fmt.Errorf("Error retrieving IoT Device Provisioning Service Certificate %q (Device Provisioning Service %q / Resource Group %q): %+v", name, iotDPSName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read IoT Device Provisioning Service Certificate %q (Device Provisioning Service %q / Resource Group %q): %+v", name, iotDPSName, resourceGroup, err) - } - - d.SetId(*resp.ID) - - return resourceArmIotDPSCertificateRead(d, meta) -} - -func resourceArmIotDPSCertificateRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).IoTHub.DPSCertificateClient - ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) - defer cancel() - - id, err := azure.ParseAzureResourceID(d.Id()) - if err != nil { - return err - } - resourceGroup := id.ResourceGroup - iotDPSName := id.Path["provisioningServices"] - name := id.Path["certificates"] - - resp, err := client.Get(ctx, name, resourceGroup, iotDPSName, "") - if err != nil { - if utils.ResponseWasNotFound(resp.Response) { - d.SetId("") - return nil - } - return fmt.Errorf("Error retrieving IoT Device Provisioning Service Certificate %q (Device Provisioning Service %q / Resource Group %q): %+v", name, iotDPSName, resourceGroup, err) - } - - d.Set("name", resp.Name) - d.Set("resource_group_name", resourceGroup) - d.Set("iot_dps_name", iotDPSName) - // We are unable to set `certificate_content` since it is not returned from the API - - return nil -} - -func resourceArmIotDPSCertificateDelete(d *schema.ResourceData, meta interface{}) error { - client := meta.(*clients.Client).IoTHub.DPSCertificateClient - ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) - defer cancel() - - id, err := azure.ParseAzureResourceID(d.Id()) - if err != nil { - return err - } - resourceGroup := id.ResourceGroup - iotDPSName := id.Path["provisioningServices"] - name := id.Path["certificates"] - - resp, err := client.Get(ctx, name, resourceGroup, iotDPSName, "") - if err != nil { - if utils.ResponseWasNotFound(resp.Response) { - return nil - } - return fmt.Errorf("Error retrieving IoT Device Provisioning Service Certificate %q (Device Provisioning Service %q / Resource Group %q): %+v", name, iotDPSName, resourceGroup, err) - } - - if resp.Etag == nil { - return fmt.Errorf("Error deleting IoT Device Provisioning Service Certificate %q (Device Provisioning Service %q / Resource Group %q) because Etag is nil", name, iotDPSName, resourceGroup) - } - - // TODO address this delete call if https://github.com/Azure/azure-rest-api-specs/pull/6311 get's merged - if _, err := client.Delete(ctx, resourceGroup, *resp.Etag, iotDPSName, name, "", nil, nil, iothub.ServerAuthentication, nil, nil, nil, ""); err != nil { - return fmt.Errorf("Error deleting IoT Device Provisioning Service Certificate %q (Device Provisioning Service %q / Resource Group %q): %+v", name, iotDPSName, resourceGroup, err) - } - return nil -} diff --git a/azurerm/internal/services/iothub/tests/resource_arm_iot_dps_certificate_test.go b/azurerm/internal/services/iothub/tests/resource_arm_iot_dps_certificate_test.go deleted file mode 100644 index 03bf322e7f41..000000000000 --- a/azurerm/internal/services/iothub/tests/resource_arm_iot_dps_certificate_test.go +++ /dev/null @@ -1,216 +0,0 @@ -package tests - -import ( - "fmt" - "net/http" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" -) - -func TestAccAzureRMIotDPSCertificate_basic(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_iot_dps_certificate", "test") - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.PreCheck(t) }, - Providers: acceptance.SupportedProviders, - CheckDestroy: testCheckAzureRMIotDPSCertificateDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAzureRMIotDPSCertificate_basic(data), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMIotDPSCertificateExists(data.ResourceName), - ), - }, - data.ImportStep("certificate_content"), - }, - }) -} - -func TestAccAzureRMIotDPSCertificate_requiresImport(t *testing.T) { - if !features.ShouldResourcesBeImported() { - t.Skip("Skipping since resources aren't required to be imported") - return - } - - data := acceptance.BuildTestData(t, "azurerm_iot_dps_certificate", "test") - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.PreCheck(t) }, - Providers: acceptance.SupportedProviders, - CheckDestroy: testCheckAzureRMIotDPSCertificateDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAzureRMIotDPSCertificate_basic(data), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMIotDPSCertificateExists(data.ResourceName), - ), - }, - { - Config: testAccAzureRMIotDPSCertificate_requiresImport(data), - ExpectError: acceptance.RequiresImportError("azurerm_iotdps"), - }, - }, - }) -} - -func TestAccAzureRMIotDPSCertificate_update(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_iot_dps_certificate", "test") - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.PreCheck(t) }, - Providers: acceptance.SupportedProviders, - CheckDestroy: testCheckAzureRMIotDPSCertificateDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAzureRMIotDPSCertificate_basic(data), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMIotDPSCertificateExists(data.ResourceName), - ), - }, - data.ImportStep("certificate_content"), - { - Config: testAccAzureRMIotDPSCertificate_update(data), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMIotDPSCertificateExists(data.ResourceName), - ), - }, - data.ImportStep("certificate_content"), - }, - }) -} - -func testCheckAzureRMIotDPSCertificateDestroy(s *terraform.State) error { - client := acceptance.AzureProvider.Meta().(*clients.Client).IoTHub.DPSCertificateClient - ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext - - for _, rs := range s.RootModule().Resources { - if rs.Type != "azurerm_iot_dps_certificate" { - continue - } - - name := rs.Primary.Attributes["name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] - iotDPSName := rs.Primary.Attributes["iot_dps_name"] - - resp, err := client.Get(ctx, name, resourceGroup, iotDPSName, "") - - if err != nil { - return nil - } - - if resp.StatusCode != http.StatusNotFound { - return fmt.Errorf("IoT Device Provisioning Service Certificate %s still exists in (device provisioning service %s / resource group %s)", name, iotDPSName, resourceGroup) - } - } - return nil -} - -func testCheckAzureRMIotDPSCertificateExists(resourceName string) resource.TestCheckFunc { - return func(s *terraform.State) error { - client := acceptance.AzureProvider.Meta().(*clients.Client).IoTHub.DPSCertificateClient - ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext - - rs, ok := s.RootModule().Resources[resourceName] - if !ok { - return fmt.Errorf("Not found: %s", resourceName) - } - name := rs.Primary.Attributes["name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - iotDPSName := rs.Primary.Attributes["iot_dps_name"] - - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for IoT Device Provisioning Service Certificate: %s", name) - } - - resp, err := client.Get(ctx, name, resourceGroup, iotDPSName, "") - if err != nil { - if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: IoT Device Provisioning Service Certificate %q (Device Provisioning Service %q / Resource Group %q) does not exist", name, iotDPSName, resourceGroup) - } - - return fmt.Errorf("Bad: Get on iothubDPSCertificateClient: %+v", err) - } - - return nil - } -} - -func testAccAzureRMIotDPSCertificate_basic(data acceptance.TestData) string { - return fmt.Sprintf(` -resource "azurerm_resource_group" "test" { - name = "acctestRG-%d" - location = "%s" -} - -resource "azurerm_iot_dps" "test" { - name = "acctestIoTDPS-%d" - resource_group_name = "${azurerm_resource_group.test.name}" - location = "${azurerm_resource_group.test.location}" - - sku { - name = "S1" - capacity = "1" - } -} - -resource "azurerm_iot_dps_certificate" "test" { - name = "acctestIoTDPSCertificate-%d" - resource_group_name = "${azurerm_resource_group.test.name}" - iot_dps_name = "${azurerm_iot_dps.test.name}" - - certificate_content = "${filebase64("testdata/batch_certificate.cer")}" -} -`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) -} - -func testAccAzureRMIotDPSCertificate_requiresImport(data acceptance.TestData) string { - template := testAccAzureRMIotDPS_basic(data) - return fmt.Sprintf(` -%s - -resource "azurerm_iot_dps_certificate" "test" { - name = "${azurerm_iot_dps_certificate.test.name}" - resource_group_name = "${azurerm_resource_group.test.name}" - iot_dps_name = "${azurerm_iot_dps.test.name}" - - certificate_content = "${filebase64("testdata/batch_certificate.cer")}" -} -`, template) -} - -func testAccAzureRMIotDPSCertificate_update(data acceptance.TestData) string { - return fmt.Sprintf(` -resource "azurerm_resource_group" "test" { - name = "acctestRG-%d" - location = "%s" -} - -resource "azurerm_iot_dps" "test" { - name = "acctestIoTDPS-%d" - resource_group_name = "${azurerm_resource_group.test.name}" - location = "${azurerm_resource_group.test.location}" - - sku { - name = "S1" - capacity = "1" - } - - tags = { - purpose = "testing" - } -} - -resource "azurerm_iot_dps_certificate" "test" { - name = "acctestIoTDPSCertificate-%d" - resource_group_name = "${azurerm_resource_group.test.name}" - iot_dps_name = "${azurerm_iot_dps.test.name}" - - certificate_content = "${filebase64("testdata/application_gateway_test.cer")}" -} -`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) -} diff --git a/azurerm/internal/services/iothub/tests/resource_arm_iot_dps_test.go b/azurerm/internal/services/iothub/tests/resource_arm_iot_dps_test.go deleted file mode 100644 index 8283155704e4..000000000000 --- a/azurerm/internal/services/iothub/tests/resource_arm_iot_dps_test.go +++ /dev/null @@ -1,284 +0,0 @@ -package tests - -import ( - "fmt" - "net/http" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" -) - -func TestAccAzureRMIotDPS_basic(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_iot_dps", "test") - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.PreCheck(t) }, - Providers: acceptance.SupportedProviders, - CheckDestroy: testCheckAzureRMIotDPSDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAzureRMIotDPS_basic(data), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMIotDPSExists(data.ResourceName), - ), - }, - data.ImportStep(), - }, - }) -} - -func TestAccAzureRMIotDPS_requiresImport(t *testing.T) { - if !features.ShouldResourcesBeImported() { - t.Skip("Skipping since resources aren't required to be imported") - return - } - - data := acceptance.BuildTestData(t, "azurerm_iot_dps", "test") - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.PreCheck(t) }, - Providers: acceptance.SupportedProviders, - CheckDestroy: testCheckAzureRMIotDPSDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAzureRMIotDPS_basic(data), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMIotDPSExists(data.ResourceName), - ), - }, - { - Config: testAccAzureRMIotDPS_requiresImport(data), - ExpectError: acceptance.RequiresImportError("azurerm_iotdps"), - }, - }, - }) -} - -func TestAccAzureRMIotDPS_update(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_iot_dps", "test") - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.PreCheck(t) }, - Providers: acceptance.SupportedProviders, - CheckDestroy: testCheckAzureRMIotDPSDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAzureRMIotDPS_basic(data), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMIotDPSExists(data.ResourceName), - ), - }, - data.ImportStep(), - { - Config: testAccAzureRMIotDPS_update(data), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMIotDPSExists(data.ResourceName), - ), - }, - data.ImportStep(), - }, - }) -} - -func TestAccAzureRMIotDPS_linkedHubs(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_iot_dps", "test") - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.PreCheck(t) }, - Providers: acceptance.SupportedProviders, - CheckDestroy: testCheckAzureRMIotDPSDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAzureRMIotDPS_linkedHubs(data), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMIotDPSExists(data.ResourceName), - ), - }, - data.ImportStep(), - { - Config: testAccAzureRMIotDPS_linkedHubsUpdated(data), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMIotDPSExists(data.ResourceName), - ), - }, - data.ImportStep(), - }, - }) -} - -func testCheckAzureRMIotDPSDestroy(s *terraform.State) error { - client := acceptance.AzureProvider.Meta().(*clients.Client).IoTHub.DPSResourceClient - ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext - - for _, rs := range s.RootModule().Resources { - if rs.Type != "azurerm_iotdps" { - continue - } - - name := rs.Primary.Attributes["name"] - resourceGroup := rs.Primary.Attributes["resource_group_name"] - - resp, err := client.Get(ctx, resourceGroup, name) - - if err != nil { - return nil - } - - if resp.StatusCode != http.StatusNotFound { - return fmt.Errorf("IoT Device Provisioning Service %s still exists in resource group %s", name, resourceGroup) - } - } - return nil -} - -func testCheckAzureRMIotDPSExists(resourceName string) resource.TestCheckFunc { - return func(s *terraform.State) error { - ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext - client := acceptance.AzureProvider.Meta().(*clients.Client).IoTHub.DPSResourceClient - - rs, ok := s.RootModule().Resources[resourceName] - if !ok { - return fmt.Errorf("Not found: %s", resourceName) - } - iotdpsName := rs.Primary.Attributes["name"] - resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] - if !hasResourceGroup { - return fmt.Errorf("Bad: no resource group found in state for IoT Device Provisioning Service: %s", iotdpsName) - } - - resp, err := client.Get(ctx, iotdpsName, resourceGroup) - if err != nil { - if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("Bad: IoT Device Provisioning Service %q (Resource Group %q) does not exist", iotdpsName, resourceGroup) - } - - return fmt.Errorf("Bad: Get on iothubDPSResourceClient: %+v", err) - } - - return nil - } -} - -func testAccAzureRMIotDPS_basic(data acceptance.TestData) string { - return fmt.Sprintf(` -resource "azurerm_resource_group" "test" { - name = "acctestRG-%d" - location = "%s" -} - -resource "azurerm_iot_dps" "test" { - name = "acctestIoTDPS-%d" - resource_group_name = "${azurerm_resource_group.test.name}" - location = "${azurerm_resource_group.test.location}" - - sku { - name = "S1" - capacity = "1" - } -} -`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) -} - -func testAccAzureRMIotDPS_requiresImport(data acceptance.TestData) string { - template := testAccAzureRMIotDPS_basic(data) - return fmt.Sprintf(` -%s - -resource "azurerm_iot_dps" "import" { - name = "${azurerm_iot_dps.test.name}" - resource_group_name = "${azurerm_iot_dps.test.resource_group_name}" - location = "${azurerm_iot_dps.test.location}" - - sku { - name = "S1" - capacity = "1" - } -} -`, template) -} - -func testAccAzureRMIotDPS_update(data acceptance.TestData) string { - return fmt.Sprintf(` -resource "azurerm_resource_group" "test" { - name = "acctestRG-%d" - location = "%s" -} - -resource "azurerm_iot_dps" "test" { - name = "acctestIoTDPS-%d" - resource_group_name = "${azurerm_resource_group.test.name}" - location = "${azurerm_resource_group.test.location}" - - sku { - name = "S1" - capacity = "1" - } - - tags = { - purpose = "testing" - } -} -`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) -} - -func testAccAzureRMIotDPS_linkedHubs(data acceptance.TestData) string { - return fmt.Sprintf(` -resource "azurerm_resource_group" "test" { - name = "acctestRG-%d" - location = "%s" -} - -resource "azurerm_iot_dps" "test" { - name = "acctestIoTDPS-%d" - resource_group_name = "${azurerm_resource_group.test.name}" - location = "${azurerm_resource_group.test.location}" - - sku { - name = "S1" - capacity = "1" - } - - linked_hub { - connection_string = "HostName=test.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=booo" - location = "${azurerm_resource_group.test.location}" - allocation_weight = 15 - apply_allocation_policy = true - } - - linked_hub { - connection_string = "HostName=test2.azure-devices.net;SharedAccessKeyName=iothubowner2;SharedAccessKey=key2" - location = "${azurerm_resource_group.test.location}" - } -} -`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) -} - -func testAccAzureRMIotDPS_linkedHubsUpdated(data acceptance.TestData) string { - return fmt.Sprintf(` -resource "azurerm_resource_group" "test" { - name = "acctestRG-%d" - location = "%s" -} - -resource "azurerm_iot_dps" "test" { - name = "acctestIoTDPS-%d" - resource_group_name = "${azurerm_resource_group.test.name}" - location = "${azurerm_resource_group.test.location}" - - sku { - name = "S1" - capacity = "1" - } - - linked_hub { - connection_string = "HostName=test.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=booo" - location = "${azurerm_resource_group.test.location}" - allocation_weight = 150 - } -} -`, data.RandomInteger, data.Locations.Primary, data.RandomInteger) -} diff --git a/azurerm/internal/services/iothub/tests/resource_arm_iothub_dps_test.go b/azurerm/internal/services/iothub/tests/resource_arm_iothub_dps_test.go index 2219458d2d30..d56e7fa952b2 100644 --- a/azurerm/internal/services/iothub/tests/resource_arm_iothub_dps_test.go +++ b/azurerm/internal/services/iothub/tests/resource_arm_iothub_dps_test.go @@ -46,7 +46,7 @@ func TestAccAzureRMIotHubDPS_requiresImport(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acceptance.PreCheck(t) }, Providers: acceptance.SupportedProviders, - CheckDestroy: testCheckAzureRMIotDPSDestroy, + CheckDestroy: testCheckAzureRMIotHubDPSDestroy, Steps: []resource.TestStep{ { Config: testAccAzureRMIotHubDPS_basic(data), @@ -68,7 +68,7 @@ func TestAccAzureRMIotHubDPS_update(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acceptance.PreCheck(t) }, Providers: acceptance.SupportedProviders, - CheckDestroy: testCheckAzureRMIotDPSDestroy, + CheckDestroy: testCheckAzureRMIotHubDPSDestroy, Steps: []resource.TestStep{ { Config: testAccAzureRMIotHubDPS_basic(data),