diff --git a/azurerm/data_source_automation_account.go b/azurerm/data_source_automation_account.go new file mode 100644 index 000000000000..012b74c09110 --- /dev/null +++ b/azurerm/data_source_automation_account.go @@ -0,0 +1,65 @@ +package azurerm + +import ( + "fmt" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmAutomationAccount() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAutomationAccountRead, + + Timeouts: &schema.ResourceTimeout{ + Read: schema.DefaultTimeout(5 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + + "resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(), + + "primary_key": { + Type: schema.TypeString, + Computed: true, + }, + "secondary_key": { + Type: schema.TypeString, + Computed: true, + }, + "endpoint": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceAutomationAccountRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).Automation.AgentRegistrationInfoClient + ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d) + defer cancel() + + name := d.Get("name").(string) + resourceGroupName := d.Get("resource_group_name").(string) + + resp, err := client.Get(ctx, resourceGroupName, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Error: Automation Account %q (Resource Group %q) was not found", name, resourceGroupName) + } + return fmt.Errorf("Error making Read request on Automation Account Registration Information %q (Resource Group %q): %+v", name, resourceGroupName, err) + } + d.SetId(*resp.ID) + d.Set("primary_key", resp.Keys.Primary) + d.Set("secondary_key", resp.Keys.Secondary) + d.Set("endpoint", resp.Endpoint) + return nil +} diff --git a/azurerm/data_source_automation_account_test.go b/azurerm/data_source_automation_account_test.go new file mode 100644 index 000000000000..958dff642e58 --- /dev/null +++ b/azurerm/data_source_automation_account_test.go @@ -0,0 +1,48 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" +) + +func TestAccDataSourceAutomationAccount(t *testing.T) { + dataSourceName := "data.azurerm_automation_account.test" + ri := tf.AccRandTimeInt() + location := testLocation() + resourceGroupName := fmt.Sprintf("acctestRG-%d", ri) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAutomationAccount_complete(resourceGroupName, location, ri), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(dataSourceName, "resource_group_name", resourceGroupName), + ), + }, + }, + }) +} + +func testAccDataSourceAutomationAccount_complete(resourceGroupName string, location string, ri int) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "%s" + location = "%s" +} +resource "azurerm_automation_account" "test" { + name = "acctestautomationAccount-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + sku_name = "Basic" +} +data "azurerm_automation_account" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + name = "${azurerm_automation_account.test.name}" +} +`, resourceGroupName, location, ri) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index b37dbde356b5..7eb20555bcaf 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -63,6 +63,7 @@ func Provider() terraform.ResourceProvider { "azurerm_app_service_certificate_order": dataSourceArmAppServiceCertificateOrder(), "azurerm_application_insights": dataSourceArmApplicationInsights(), "azurerm_application_security_group": dataSourceArmApplicationSecurityGroup(), + "azurerm_automation_account": dataSourceArmAutomationAccount(), "azurerm_automation_variable_bool": dataSourceArmAutomationVariableBool(), "azurerm_automation_variable_datetime": dataSourceArmAutomationVariableDateTime(), "azurerm_automation_variable_int": dataSourceArmAutomationVariableInt(), diff --git a/website/azurerm.erb b/website/azurerm.erb index 7859ebbb6087..917a0aefc57a 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -90,6 +90,10 @@ azurerm_application_insights +