From 0a6051f4e62b85047df991e6d2718569dbe05a8e Mon Sep 17 00:00:00 2001 From: Matias Charriere Date: Wed, 27 Feb 2019 17:16:43 -0300 Subject: [PATCH 1/4] feat/recoveryservicepolicy: added protection policy vm data source --- ..._recovery_services_protection_policy_vm.go | 60 ++++++++ azurerm/provider.go | 131 +++++++++--------- 2 files changed, 126 insertions(+), 65 deletions(-) create mode 100644 azurerm/data_source_recovery_services_protection_policy_vm.go diff --git a/azurerm/data_source_recovery_services_protection_policy_vm.go b/azurerm/data_source_recovery_services_protection_policy_vm.go new file mode 100644 index 000000000000..10eb6b6a8923 --- /dev/null +++ b/azurerm/data_source_recovery_services_protection_policy_vm.go @@ -0,0 +1,60 @@ +package azurerm + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmRecoveryServicesProtectionPolicyVm() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmRecoveryServicesProtectionPolicyVmRead, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "recovery_vault_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "resource_group_name": resourceGroupNameForDataSourceSchema(), + + "tags": tagsForDataSourceSchema(), + }, + } +} + +func dataSourceArmRecoveryServicesProtectionPolicyVmRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).recoveryServicesProtectionPoliciesClient + ctx := meta.(*ArmClient).StopContext + + name := d.Get("name").(string) + resourceGroup := d.Get("resource_group_name").(string) + vaultName := d.Get("recovery_vault_name").(string) + + log.Printf("[DEBUG] Reading Recovery Service Protection Policy Vm %q (resource group %q)", name, resourceGroup) + + protectionPolicy, err := client.Get(ctx, vaultName, resourceGroup, name) + if err != nil { + if utils.ResponseWasNotFound(protectionPolicy.Response) { + return fmt.Errorf("Error: Recovery Services Protection Policy %q (Resource Group %q) was not found", name, resourceGroup) + } + + return fmt.Errorf("Error making Read request on Recovery Service Protection Policy %q (Resource Group %q): %+v", name, resourceGroup, err) + } + + d.SetId(*protectionPolicy.ID) + d.Set("name", protectionPolicy.Name) + d.Set("resource_group_name", resourceGroup) + + flattenAndSetTags(d, protectionPolicy.Tags) + return nil +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 9429b84147b4..cd4ca94228d2 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -98,71 +98,72 @@ func Provider() terraform.ResourceProvider { }, DataSourcesMap: map[string]*schema.Resource{ - "azurerm_api_management": dataSourceApiManagementService(), - "azurerm_api_management_group": dataSourceApiManagementGroup(), - "azurerm_api_management_product": dataSourceApiManagementProduct(), - "azurerm_api_management_user": dataSourceArmApiManagementUser(), - "azurerm_app_service_plan": dataSourceAppServicePlan(), - "azurerm_app_service": dataSourceArmAppService(), - "azurerm_application_insights": dataSourceArmApplicationInsights(), - "azurerm_application_security_group": dataSourceArmApplicationSecurityGroup(), - "azurerm_availability_set": dataSourceArmAvailabilitySet(), - "azurerm_azuread_application": dataSourceArmAzureADApplication(), - "azurerm_azuread_service_principal": dataSourceArmActiveDirectoryServicePrincipal(), - "azurerm_batch_account": dataSourceArmBatchAccount(), - "azurerm_batch_pool": dataSourceArmBatchPool(), - "azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(), - "azurerm_cdn_profile": dataSourceArmCdnProfile(), - "azurerm_client_config": dataSourceArmClientConfig(), - "azurerm_container_registry": dataSourceArmContainerRegistry(), - "azurerm_cosmosdb_account": dataSourceArmCosmosDBAccount(), - "azurerm_data_lake_store": dataSourceArmDataLakeStoreAccount(), - "azurerm_dev_test_lab": dataSourceArmDevTestLab(), - "azurerm_dns_zone": dataSourceArmDnsZone(), - "azurerm_eventhub_namespace": dataSourceEventHubNamespace(), - "azurerm_image": dataSourceArmImage(), - "azurerm_key_vault_access_policy": dataSourceArmKeyVaultAccessPolicy(), - "azurerm_key_vault_key": dataSourceArmKeyVaultKey(), - "azurerm_key_vault_secret": dataSourceArmKeyVaultSecret(), - "azurerm_key_vault": dataSourceArmKeyVault(), - "azurerm_kubernetes_cluster": dataSourceArmKubernetesCluster(), - "azurerm_lb": dataSourceArmLoadBalancer(), - "azurerm_lb_backend_address_pool": dataSourceArmLoadBalancerBackendAddressPool(), - "azurerm_log_analytics_workspace": dataSourceLogAnalyticsWorkspace(), - "azurerm_logic_app_workflow": dataSourceArmLogicAppWorkflow(), - "azurerm_managed_disk": dataSourceArmManagedDisk(), - "azurerm_management_group": dataSourceArmManagementGroup(), - "azurerm_monitor_action_group": dataSourceArmMonitorActionGroup(), - "azurerm_monitor_diagnostic_categories": dataSourceArmMonitorDiagnosticCategories(), - "azurerm_monitor_log_profile": dataSourceArmMonitorLogProfile(), - "azurerm_network_interface": dataSourceArmNetworkInterface(), - "azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(), - "azurerm_network_watcher": dataSourceArmNetworkWatcher(), - "azurerm_notification_hub_namespace": dataSourceNotificationHubNamespace(), - "azurerm_notification_hub": dataSourceNotificationHub(), - "azurerm_platform_image": dataSourceArmPlatformImage(), - "azurerm_policy_definition": dataSourceArmPolicyDefinition(), - "azurerm_public_ip": dataSourceArmPublicIP(), - "azurerm_public_ips": dataSourceArmPublicIPs(), - "azurerm_recovery_services_vault": dataSourceArmRecoveryServicesVault(), - "azurerm_resource_group": dataSourceArmResourceGroup(), - "azurerm_role_definition": dataSourceArmRoleDefinition(), - "azurerm_route_table": dataSourceArmRouteTable(), - "azurerm_scheduler_job_collection": dataSourceArmSchedulerJobCollection(), - "azurerm_servicebus_namespace": dataSourceArmServiceBusNamespace(), - "azurerm_shared_image_gallery": dataSourceArmSharedImageGallery(), - "azurerm_shared_image_version": dataSourceArmSharedImageVersion(), - "azurerm_shared_image": dataSourceArmSharedImage(), - "azurerm_snapshot": dataSourceArmSnapshot(), - "azurerm_storage_account_sas": dataSourceArmStorageAccountSharedAccessSignature(), - "azurerm_storage_account": dataSourceArmStorageAccount(), - "azurerm_subnet": dataSourceArmSubnet(), - "azurerm_subscription": dataSourceArmSubscription(), - "azurerm_subscriptions": dataSourceArmSubscriptions(), - "azurerm_traffic_manager_geographical_location": dataSourceArmTrafficManagerGeographicalLocation(), - "azurerm_virtual_machine": dataSourceArmVirtualMachine(), - "azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(), - "azurerm_virtual_network": dataSourceArmVirtualNetwork(), + "azurerm_api_management": dataSourceApiManagementService(), + "azurerm_api_management_group": dataSourceApiManagementGroup(), + "azurerm_api_management_product": dataSourceApiManagementProduct(), + "azurerm_api_management_user": dataSourceArmApiManagementUser(), + "azurerm_app_service_plan": dataSourceAppServicePlan(), + "azurerm_app_service": dataSourceArmAppService(), + "azurerm_application_insights": dataSourceArmApplicationInsights(), + "azurerm_application_security_group": dataSourceArmApplicationSecurityGroup(), + "azurerm_availability_set": dataSourceArmAvailabilitySet(), + "azurerm_azuread_application": dataSourceArmAzureADApplication(), + "azurerm_azuread_service_principal": dataSourceArmActiveDirectoryServicePrincipal(), + "azurerm_batch_account": dataSourceArmBatchAccount(), + "azurerm_batch_pool": dataSourceArmBatchPool(), + "azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(), + "azurerm_cdn_profile": dataSourceArmCdnProfile(), + "azurerm_client_config": dataSourceArmClientConfig(), + "azurerm_container_registry": dataSourceArmContainerRegistry(), + "azurerm_cosmosdb_account": dataSourceArmCosmosDBAccount(), + "azurerm_data_lake_store": dataSourceArmDataLakeStoreAccount(), + "azurerm_dev_test_lab": dataSourceArmDevTestLab(), + "azurerm_dns_zone": dataSourceArmDnsZone(), + "azurerm_eventhub_namespace": dataSourceEventHubNamespace(), + "azurerm_image": dataSourceArmImage(), + "azurerm_key_vault_access_policy": dataSourceArmKeyVaultAccessPolicy(), + "azurerm_key_vault_key": dataSourceArmKeyVaultKey(), + "azurerm_key_vault_secret": dataSourceArmKeyVaultSecret(), + "azurerm_key_vault": dataSourceArmKeyVault(), + "azurerm_kubernetes_cluster": dataSourceArmKubernetesCluster(), + "azurerm_lb": dataSourceArmLoadBalancer(), + "azurerm_lb_backend_address_pool": dataSourceArmLoadBalancerBackendAddressPool(), + "azurerm_log_analytics_workspace": dataSourceLogAnalyticsWorkspace(), + "azurerm_logic_app_workflow": dataSourceArmLogicAppWorkflow(), + "azurerm_managed_disk": dataSourceArmManagedDisk(), + "azurerm_management_group": dataSourceArmManagementGroup(), + "azurerm_monitor_action_group": dataSourceArmMonitorActionGroup(), + "azurerm_monitor_diagnostic_categories": dataSourceArmMonitorDiagnosticCategories(), + "azurerm_monitor_log_profile": dataSourceArmMonitorLogProfile(), + "azurerm_network_interface": dataSourceArmNetworkInterface(), + "azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(), + "azurerm_network_watcher": dataSourceArmNetworkWatcher(), + "azurerm_notification_hub_namespace": dataSourceNotificationHubNamespace(), + "azurerm_notification_hub": dataSourceNotificationHub(), + "azurerm_platform_image": dataSourceArmPlatformImage(), + "azurerm_policy_definition": dataSourceArmPolicyDefinition(), + "azurerm_public_ip": dataSourceArmPublicIP(), + "azurerm_public_ips": dataSourceArmPublicIPs(), + "azurerm_recovery_services_vault": dataSourceArmRecoveryServicesVault(), + "azurerm_recovery_services_protection_policy_vm": dataSourceArmRecoveryServicesProtectionPolicyVm(), + "azurerm_resource_group": dataSourceArmResourceGroup(), + "azurerm_role_definition": dataSourceArmRoleDefinition(), + "azurerm_route_table": dataSourceArmRouteTable(), + "azurerm_scheduler_job_collection": dataSourceArmSchedulerJobCollection(), + "azurerm_servicebus_namespace": dataSourceArmServiceBusNamespace(), + "azurerm_shared_image_gallery": dataSourceArmSharedImageGallery(), + "azurerm_shared_image_version": dataSourceArmSharedImageVersion(), + "azurerm_shared_image": dataSourceArmSharedImage(), + "azurerm_snapshot": dataSourceArmSnapshot(), + "azurerm_storage_account_sas": dataSourceArmStorageAccountSharedAccessSignature(), + "azurerm_storage_account": dataSourceArmStorageAccount(), + "azurerm_subnet": dataSourceArmSubnet(), + "azurerm_subscription": dataSourceArmSubscription(), + "azurerm_subscriptions": dataSourceArmSubscriptions(), + "azurerm_traffic_manager_geographical_location": dataSourceArmTrafficManagerGeographicalLocation(), + "azurerm_virtual_machine": dataSourceArmVirtualMachine(), + "azurerm_virtual_network_gateway": dataSourceArmVirtualNetworkGateway(), + "azurerm_virtual_network": dataSourceArmVirtualNetwork(), }, ResourcesMap: map[string]*schema.Resource{ From 64ada74929eb0b5b5fe83e82f241309641e82688 Mon Sep 17 00:00:00 2001 From: Matias Charriere Date: Wed, 27 Feb 2019 17:32:07 -0300 Subject: [PATCH 2/4] feat/recoveryservicepolicy: added tests for data source --- ...very_services_protection_policy_vm_test.go | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 azurerm/data_source_recovery_services_protection_policy_vm_test.go diff --git a/azurerm/data_source_recovery_services_protection_policy_vm_test.go b/azurerm/data_source_recovery_services_protection_policy_vm_test.go new file mode 100644 index 000000000000..3babc382ba8a --- /dev/null +++ b/azurerm/data_source_recovery_services_protection_policy_vm_test.go @@ -0,0 +1,44 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccDataSourceAzureRMRecoveryServicesProtectionPolicyVm_basic(t *testing.T) { + dataSourceName := "data.azurerm_recovery_services_protection_policy_vm.test" + ri := tf.AccRandTimeInt() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceRecoveryServicesProtectionPolicyVm_basic(ri, testLocation()), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMRecoveryServicesProtectionPolicyVmExists(dataSourceName), + resource.TestCheckResourceAttrSet(dataSourceName, "name"), + resource.TestCheckResourceAttrSet(dataSourceName, "recovery_vault_name"), + resource.TestCheckResourceAttrSet(dataSourceName, "location"), + resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"), + resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"), + ), + }, + }, + }) +} + +func testAccDataSourceRecoveryServicesProtectionPolicyVm_basic(rInt int, location string) string { + return fmt.Sprintf(` +%s + +data "azurerm_recovery_services_vault" "test" { + name = "${azurerm_recovery_services_protection_policy_vm.test.name}" + recovery_vault_name = "${azurerm_recovery_services_vault.test.name}" + resource_group_name = "${azurerm_resource_group.test.name}" +} +`, testAccAzureRMRecoveryServicesProtectionPolicyVm_basicDaily(rInt, location)) +} From d3329201c502ddd50c3e0f5954368ba3f02ae27b Mon Sep 17 00:00:00 2001 From: Matias Charriere Date: Wed, 27 Feb 2019 17:53:48 -0300 Subject: [PATCH 3/4] feat/recoveryservicepolicy: added doc to data source --- ..._recovery_services_protection_policy_vm.go | 2 +- ...very_services_protection_policy_vm_test.go | 3 +- ...ery_services_protection_policy_vm.markdown | 40 +++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 website/docs/d/recovery_services_protection_policy_vm.markdown diff --git a/azurerm/data_source_recovery_services_protection_policy_vm.go b/azurerm/data_source_recovery_services_protection_policy_vm.go index 10eb6b6a8923..8849089ba439 100644 --- a/azurerm/data_source_recovery_services_protection_policy_vm.go +++ b/azurerm/data_source_recovery_services_protection_policy_vm.go @@ -40,7 +40,7 @@ func dataSourceArmRecoveryServicesProtectionPolicyVmRead(d *schema.ResourceData, resourceGroup := d.Get("resource_group_name").(string) vaultName := d.Get("recovery_vault_name").(string) - log.Printf("[DEBUG] Reading Recovery Service Protection Policy Vm %q (resource group %q)", name, resourceGroup) + log.Printf("[DEBUG] Reading Recovery Service Protection Policy %q (resource group %q)", name, resourceGroup) protectionPolicy, err := client.Get(ctx, vaultName, resourceGroup, name) if err != nil { diff --git a/azurerm/data_source_recovery_services_protection_policy_vm_test.go b/azurerm/data_source_recovery_services_protection_policy_vm_test.go index 3babc382ba8a..fce8fe97b1fc 100644 --- a/azurerm/data_source_recovery_services_protection_policy_vm_test.go +++ b/azurerm/data_source_recovery_services_protection_policy_vm_test.go @@ -22,7 +22,6 @@ func TestAccDataSourceAzureRMRecoveryServicesProtectionPolicyVm_basic(t *testing testCheckAzureRMRecoveryServicesProtectionPolicyVmExists(dataSourceName), resource.TestCheckResourceAttrSet(dataSourceName, "name"), resource.TestCheckResourceAttrSet(dataSourceName, "recovery_vault_name"), - resource.TestCheckResourceAttrSet(dataSourceName, "location"), resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"), resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"), ), @@ -35,7 +34,7 @@ func testAccDataSourceRecoveryServicesProtectionPolicyVm_basic(rInt int, locatio return fmt.Sprintf(` %s -data "azurerm_recovery_services_vault" "test" { +data "azurerm_recovery_services_protection_policy_vm" "test" { name = "${azurerm_recovery_services_protection_policy_vm.test.name}" recovery_vault_name = "${azurerm_recovery_services_vault.test.name}" resource_group_name = "${azurerm_resource_group.test.name}" diff --git a/website/docs/d/recovery_services_protection_policy_vm.markdown b/website/docs/d/recovery_services_protection_policy_vm.markdown new file mode 100644 index 000000000000..7d68c4931c66 --- /dev/null +++ b/website/docs/d/recovery_services_protection_policy_vm.markdown @@ -0,0 +1,40 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_recovery_services_protection_policy_vm" +sidebar_current: "docs-azurerm-datasource-recovery-services-protection_policy_vm" +description: |- + Gets information about an existing Recovery Services VM Protection Policy. +--- + +# Data Source: azurerm_recovery_services_protection_policy_vm + +Use this data source to access information about an existing Recovery Services VM Protection Policy. + +## Example Usage + +```hcl +data "azurerm_recovery_services_protection_policy_vm" "policy" { + name = "policy" + recovery_vault_name = "recovery_vault" + resource_group_name = "resource_group" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Specifies the name of the Recovery Services VM Protection Policy. + +* `recovery_vault_name` - (Required) Specifies the name of the Recovery Services Vault. + +* `resource_group_name` - (Required) The name of the resource group in which the Recovery Services VM Protection Policy resides. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the Recovery Services VM Protection Policy. + +* `tags` - A mapping of tags assigned to the resource. + From 30a837e0232331145ccf24903098cdd3bec8b365 Mon Sep 17 00:00:00 2001 From: Matias Charriere Date: Thu, 28 Feb 2019 09:22:58 -0300 Subject: [PATCH 4/4] feat/recoveryservicepolicy: fixed comments --- azurerm/data_source_recovery_services_protection_policy_vm.go | 2 -- website/docs/d/recovery_services_protection_policy_vm.markdown | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/azurerm/data_source_recovery_services_protection_policy_vm.go b/azurerm/data_source_recovery_services_protection_policy_vm.go index 8849089ba439..cde01ed20418 100644 --- a/azurerm/data_source_recovery_services_protection_policy_vm.go +++ b/azurerm/data_source_recovery_services_protection_policy_vm.go @@ -52,8 +52,6 @@ func dataSourceArmRecoveryServicesProtectionPolicyVmRead(d *schema.ResourceData, } d.SetId(*protectionPolicy.ID) - d.Set("name", protectionPolicy.Name) - d.Set("resource_group_name", resourceGroup) flattenAndSetTags(d, protectionPolicy.Tags) return nil diff --git a/website/docs/d/recovery_services_protection_policy_vm.markdown b/website/docs/d/recovery_services_protection_policy_vm.markdown index 7d68c4931c66..3b6ce1b6e44c 100644 --- a/website/docs/d/recovery_services_protection_policy_vm.markdown +++ b/website/docs/d/recovery_services_protection_policy_vm.markdown @@ -1,7 +1,7 @@ --- layout: "azurerm" page_title: "Azure Resource Manager: azurerm_recovery_services_protection_policy_vm" -sidebar_current: "docs-azurerm-datasource-recovery-services-protection_policy_vm" +sidebar_current: "docs-azurerm-datasource-recovery-services-protection-policy-vm" description: |- Gets information about an existing Recovery Services VM Protection Policy. ---