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

New Data Source: azurerm_automation_runbook - #26359

Merged
merged 5 commits into from
Jun 20, 2024
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
170 changes: 170 additions & 0 deletions internal/services/automation/automation_runbook_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package automation

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2023-11-01/runbook"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automation/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type AutomationRunbookDataSource struct{}

type AutomationRunbookDataSourceModel struct {
RunbookName string `tfschema:"name"`
AutomationAccountName string `tfschema:"automation_account_name"`
ResourceGroupName string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
Description string `tfschema:"description"`
LogProgress bool `tfschema:"log_progress"`
LogVerbose bool `tfschema:"log_verbose"`
RunbookType string `tfschema:"runbook_type"`
LogActivityTrace int64 `tfschema:"log_activity_trace_level"`
Content string `tfschema:"content"`
Tags map[string]string `tfschema:"tags "`
}

var _ sdk.DataSource = AutomationRunbookDataSource{}

func (d AutomationRunbookDataSource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.RunbookName(),
},

"automation_account_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.AutomationAccount(),
},

"resource_group_name": commonschema.ResourceGroupNameForDataSource(),
}
}

func (d AutomationRunbookDataSource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"location": commonschema.LocationComputed(),

"description": {
Type: pluginsdk.TypeString,
Computed: true,
},

"log_progress": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"log_verbose": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"runbook_type": {
Type: pluginsdk.TypeString,
Computed: true,
},

"log_activity_trace_level": {
Type: pluginsdk.TypeInt,
Computed: true,
},

"content": {
Type: pluginsdk.TypeString,
Computed: true,
},

"tags": commonschema.TagsDataSource(),
}
}

func (d AutomationRunbookDataSource) ModelObject() interface{} {
return &AutomationRunbookDataSourceModel{}
}

func (d AutomationRunbookDataSource) ResourceType() string {
return "azurerm_automation_runbook"
}

func (d AutomationRunbookDataSource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Automation.Runbook
subscriptionId := metadata.Client.Account.SubscriptionId

var state AutomationRunbookDataSourceModel
if err := metadata.Decode(&state); err != nil {
return err
}

id := runbook.NewRunbookID(subscriptionId, state.ResourceGroupName, state.AutomationAccountName, state.RunbookName)

resp, err := client.Get(ctx, id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

contentResp, err := client.GetContent(ctx, id)
if err != nil {
if !response.WasNotFound(contentResp.HttpResponse) {
return fmt.Errorf("retrieving runbook content %s: %+v", id, err)
}
}

model := resp.Model
if model == nil {
return fmt.Errorf("retrieving %s: model was nil", id)
}

state.Location = location.NormalizeNilable(model.Location)

if model.Properties.Description != nil {
state.Description = pointer.From(model.Properties.Description)
}

if model.Properties.LogProgress != nil {
state.LogProgress = pointer.From(model.Properties.LogProgress)
}

if model.Properties.RunbookType != nil {
state.RunbookType = string(pointer.From(model.Properties.RunbookType))
}

if model.Properties.LogVerbose != nil {
state.LogVerbose = pointer.From(model.Properties.LogVerbose)
}

if model.Properties.LogActivityTrace != nil {
state.LogActivityTrace = pointer.From(model.Properties.LogActivityTrace)
}

if contentResp.Model != nil {
state.Content = string(pointer.From(contentResp.Model))
}

state.Tags = pointer.From(model.Tags)

metadata.SetID(id)

return metadata.Encode(&state)
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package automation_test

import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type AutomationRunbookDataSource struct{}

func TestAccAutomationRunbookDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_automation_runbook", "test")
r := AutomationRunbookDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("location").HasValue(location.Normalize(data.Locations.Primary)),
check.That(data.ResourceName).Key("runbook_type").HasValue("PowerShell"),
check.That(data.ResourceName).Key("content").HasValue("# Some test content\n# for Terraform acceptance test\n"),
),
},
})
}

func (AutomationRunbookDataSource) basic(data acceptance.TestData) string {
template := AutomationRunbookResource{}.PSWithContent(data)
return fmt.Sprintf(`
%s

data "azurerm_automation_runbook" "test" {
name = azurerm_automation_runbook.test.name
automation_account_name = azurerm_automation_account.test.name
resource_group_name = azurerm_resource_group.test.name
}
`, template)
}
1 change: 1 addition & 0 deletions internal/services/automation/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var _ sdk.TypedServiceRegistrationWithAGitHubLabel = Registration{}
func (r Registration) DataSources() []sdk.DataSource {
return []sdk.DataSource{
AutomationVariablesDataSource{},
AutomationRunbookDataSource{},
}
}

Expand Down
63 changes: 63 additions & 0 deletions website/docs/d/automation_runbook.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
subcategory: "Automation"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_automation_runbook"
description: |-
Gets information about an existing Automation Runbook.
---

# Data Source: azurerm_automation_runbook

Use this data source to access information about an existing Automation Runbook.

## Example Usage

```hcl
data "azurerm_automation_runbook" "example" {
name = "existing-runbook"
resource_group_name = "existing"
automation_account_name = "existing-automation"
}

output "id" {
value = data.azurerm_automation_runbook.example.id
}
```

## Arguments Reference

The following arguments are supported:

* `name` - (Required) The name of the Automation Runbook.

* `automation_account_name` - (Required) The name of the Automation Account the runbook belongs to.

* `resource_group_name` - (Required) The name of the Resource Group where the Automation exists.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The Automation Runbook ID.

* `content` - The content of the Runbook.

* `description` - The description of the Runbook.

* `location` - The Azure Region where the Runbook exists.

* `log_activity_trace_level` - The activity-level tracing of the Runbook.

* `log_progress` - The Progress log option of the Runbook.

* `log_verbose` - The Verbose log option of the Runbook.

* `runbook_type` - The type of Runbook.

* `tags` - A mapping of tags assigned to the Runbook.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the Automation.
Loading