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 resource and data source: azurerm_automation_variable_object #22644

Merged
merged 6 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 7 additions & 0 deletions internal/services/automation/automation_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package automation

import (
"encoding/json"
"fmt"
"log"
"regexp"
Expand Down Expand Up @@ -49,6 +50,9 @@ func ParseAzureAutomationVariableValue(resource string, input *string) (interfac
actualResource = "azurerm_automation_variable_int"
} else if value, err = strconv.ParseBool(*input); err == nil {
actualResource = "azurerm_automation_variable_bool"
} else if err := json.Unmarshal([]byte(*input), &value); err == nil {
value = *input
actualResource = "azurerm_automation_variable_object"
}

if actualResource != resource {
Expand Down Expand Up @@ -165,6 +169,9 @@ func resourceAutomationVariableCreateUpdate(d *pluginsdk.ResourceData, meta inte
value = strconv.FormatBool(d.Get("value").(bool))
case "int":
value = strconv.Itoa(d.Get("value").(int))
case "object":
// We don't quote the object so it gets saved as a JSON object
value = d.Get("value").(string)
case "string":
value = strconv.Quote(d.Get("value").(string))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package automation

import (
"time"

"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

func dataSourceAutomationVariableObject() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceAutomationVariableObjectRead,

Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Schema: datasourceAutomationVariableCommonSchema(pluginsdk.TypeString),
}
}

func dataSourceAutomationVariableObjectRead(d *pluginsdk.ResourceData, meta interface{}) error {
return dataSourceAutomationVariableRead(d, meta, "Object")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package automation_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type AutomationVariableObjectDataSource struct{}

func TestAccDataSourceAzureRMAutomationVariableObject_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_automation_variable_object", "test")
r := AutomationVariableObjectDataSource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("value").Exists(),
),
},
})
}

func (AutomationVariableObjectDataSource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_automation_variable_object" "test" {
name = azurerm_automation_variable_object.test.name
resource_group_name = azurerm_automation_variable_object.test.resource_group_name
automation_account_name = azurerm_automation_variable_object.test.automation_account_name
}
`, AutomationVariableObjectResource{}.basic(data))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package automation

import (
"time"

"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2022-08-08/variable"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
)

func resourceAutomationVariableObject() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceAutomationVariableObjectCreate,
Read: resourceAutomationVariableObjectRead,
Update: resourceAutomationVariableObjectUpdate,
Delete: resourceAutomationVariableObjectDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := variable.ParseVariableID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

Schema: resourceAutomationVariableCommonSchema(pluginsdk.TypeString, validation.StringIsNotEmpty),
}
}

func resourceAutomationVariableObjectCreate(d *pluginsdk.ResourceData, meta interface{}) error {
return resourceAutomationVariableCreateUpdate(d, meta, "Object")
}

func resourceAutomationVariableObjectUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
return resourceAutomationVariableCreateUpdate(d, meta, "Object")
}

func resourceAutomationVariableObjectRead(d *pluginsdk.ResourceData, meta interface{}) error {
return resourceAutomationVariableRead(d, meta, "Object")
}

func resourceAutomationVariableObjectDelete(d *pluginsdk.ResourceData, meta interface{}) error {
return resourceAutomationVariableDelete(d, meta, "Object")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package automation_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type AutomationVariableObjectResource struct{}

func TestAccAutomationVariableObject_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_automation_variable_object", "test")
r := AutomationVariableObjectResource{}

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

func TestAccAutomationVariableObject_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_automation_variable_object", "test")
r := AutomationVariableObjectResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("description").HasValue("This variable is created by Terraform acceptance test."),
),
},
data.ImportStep(),
})
}

func TestAccAutomationVariableObject_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_automation_variable_object", "test")
r := AutomationVariableObjectResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("description").HasValue("This variable is created by Terraform acceptance test."),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccAutomationVariableObject_encrypted(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_automation_variable_object", "test")
r := AutomationVariableObjectResource{}

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

func TestAccAutomationVariableObject_encryptedUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_automation_variable_object", "test")
r := AutomationVariableObjectResource{}

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

func (t AutomationVariableObjectResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
return testCheckAzureRMAutomationVariableExists(ctx, clients, state, "Object")
}

func (AutomationVariableObjectResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-auto-%d"
location = "%s"
}

resource "azurerm_automation_account" "test" {
name = "acctestAutoAcct-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku_name = "Basic"
}

resource "azurerm_automation_variable_object" "test" {
name = "acctestAutoVar-%d"
resource_group_name = azurerm_resource_group.test.name
automation_account_name = azurerm_automation_account.test.name
value = jsonencode({ property = "Hello, Terraform Basic Test." })
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func (AutomationVariableObjectResource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-auto-%d"
location = "%s"
}

resource "azurerm_automation_account" "test" {
name = "acctestAutoAcct-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku_name = "Basic"
}

resource "azurerm_automation_variable_object" "test" {
name = "acctestAutoVar-%d"
resource_group_name = azurerm_resource_group.test.name
automation_account_name = azurerm_automation_account.test.name
description = "This variable is created by Terraform acceptance test."
value = jsonencode({ property = "Hello, Terraform Complete Test." })
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}

func (AutomationVariableObjectResource) encrypted(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-auto-%d"
location = "%s"
}

resource "azurerm_automation_account" "test" {
name = "acctestAutoAcct-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku_name = "Basic"
}

resource "azurerm_automation_variable_object" "test" {
name = "acctestAutoVar-%d"
resource_group_name = azurerm_resource_group.test.name
automation_account_name = azurerm_automation_account.test.name
description = "This variable is created by Terraform acceptance test."
value = jsonencode({ property = "Hello, Terraform Encrypted Test." })
encrypted = true
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger)
}
Loading