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: azurerm_maps_creator #14566

Merged
merged 1 commit into from
Dec 17, 2021
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
6 changes: 6 additions & 0 deletions internal/services/maps/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ package client
import (
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/maps/sdk/2021-02-01/accounts"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/maps/sdk/2021-02-01/creators"
)

type Client struct {
AccountsClient *accounts.AccountsClient
CreatorsClient *creators.CreatorsClient
}

func NewClient(o *common.ClientOptions) *Client {
accountsClient := accounts.NewAccountsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&accountsClient.Client, o.ResourceManagerAuthorizer)

creatorsClient := creators.NewCreatorsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&creatorsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AccountsClient: &accountsClient,
CreatorsClient: &creatorsClient,
}
}
182 changes: 182 additions & 0 deletions internal/services/maps/maps_creator_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package maps

import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
tagsHelper "github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/maps/sdk/2021-02-01/accounts"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/maps/sdk/2021-02-01/creators"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
)

func resourceMapsCreator() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceMapsCreatorCreateUpdate,
Read: resourceMapsCreatorRead,
Update: resourceMapsCreatorUpdate,
Delete: resourceMapsCreatorDelete,

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),
},

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

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"maps_account_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: accounts.ValidateAccountID,
},

"location": azure.SchemaLocation(),

"storage_units": {
Type: pluginsdk.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(1, 100),
},

"tags": tags.Schema(),
},
}
}

func resourceMapsCreatorCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Maps.CreatorsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

accountId, err := accounts.ParseAccountID(d.Get("maps_account_id").(string))
if err != nil {
return err
}

id := creators.NewCreatorID(accountId.SubscriptionId, accountId.ResourceGroupName, accountId.AccountName, d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}

if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_maps_creator", id.ID())
}
}

location := azure.NormalizeLocation(d.Get("location"))
props := creators.Creator{
Location: location,
Properties: creators.CreatorProperties{
StorageUnits: int64(d.Get("storage_units").(int)),
},
Tags: tagsHelper.Expand(d.Get("tags").(map[string]interface{})),
}
if _, err := client.CreateOrUpdate(ctx, id, props); err != nil {

return fmt.Errorf("creating/updating %s: %+v", id, err)
}

d.SetId(id.ID())
return resourceMapsCreatorRead(d, meta)
}

func resourceMapsCreatorRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Maps.CreatorsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := creators.ParseCreatorID(d.Id())
if err != nil {
return err
}

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

d.Set("name", id.CreatorName)
d.Set("maps_account_id", accounts.NewAccountID(id.SubscriptionId, id.ResourceGroupName, id.AccountName).ID())

if model := resp.Model; model != nil {
d.Set("location", location.Normalize(model.Location))
props := model.Properties
d.Set("storage_units", props.StorageUnits)
if err := tags.FlattenAndSet(d, tagsHelper.Flatten(model.Tags)); err != nil {
return err
}
}

return nil
}

func resourceMapsCreatorUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Maps.CreatorsClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := creators.ParseCreatorID(d.Id())
if err != nil {
return err
}

props := creators.CreatorUpdateParameters{
Properties: &creators.CreatorProperties{
StorageUnits: int64(d.Get("storage_units").(int)),
},
Tags: tagsHelper.Expand(d.Get("tags").(map[string]interface{})),
}

if _, err := client.Update(ctx, *id, props); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

return resourceMapsCreatorRead(d, meta)
}

func resourceMapsCreatorDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Maps.CreatorsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := creators.ParseCreatorID(d.Id())
if err != nil {
return err
}

if _, err := client.Delete(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

return nil
}
181 changes: 181 additions & 0 deletions internal/services/maps/maps_creator_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package maps_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/response"
"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/services/maps/sdk/2021-02-01/creators"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type MapsCreatorResource struct{}

func TestAccMapsCreator_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_maps_creator", "test")
r := MapsCreatorResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccMapsCreator_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_maps_creator", "test")
r := MapsCreatorResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.RequiresImportErrorStep(r.requiresImport),
})
}

func TestAccMapsCreator_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_maps_creator", "test")
r := MapsCreatorResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccMapsCreator_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_maps_creator", "test")
r := MapsCreatorResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.update(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r MapsCreatorResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := creators.ParseCreatorID(state.ID)
if err != nil {
return nil, err
}

client := clients.Maps.CreatorsClient
resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}
return utils.Bool(true), nil
}

func (r MapsCreatorResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

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

resource "azurerm_maps_account" "test" {
name = "accMapsAccount-%d"
resource_group_name = azurerm_resource_group.test.name
sku_name = "G2"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (r MapsCreatorResource) basic(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_maps_creator" "test" {
name = "accMapsCreator-%d"
maps_account_id = azurerm_maps_account.test.id
location = "%s"
storage_units = 1
}
`, template, data.RandomInteger, data.Locations.Primary)
}

func (r MapsCreatorResource) requiresImport(data acceptance.TestData) string {
config := r.basic(data)
return fmt.Sprintf(`
%s

resource "azurerm_maps_creator" "import" {
name = azurerm_maps_creator.test.name
maps_account_id = azurerm_maps_account.test.id
location = "%s"
storage_units = 1
}
`, config, data.Locations.Primary)
}

func (r MapsCreatorResource) complete(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_maps_creator" "test" {
name = "accMapsCreator-%d"
maps_account_id = azurerm_maps_account.test.id
location = "%s"
storage_units = 1

tags = {
ENV = "Test"
}
}
`, template, data.RandomInteger, data.Locations.Primary)
}

func (r MapsCreatorResource) update(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_maps_creator" "test" {
name = "accMapsCreator-%d"
maps_account_id = azurerm_maps_account.test.id
location = "%s"
storage_units = 2

tags = {
ENV = "Test",
ENV2 = "Test2"
}
}
`, template, data.RandomInteger, data.Locations.Primary)
}
1 change: 1 addition & 0 deletions internal/services/maps/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
return map[string]*pluginsdk.Resource{
"azurerm_maps_account": resourceMapsAccount(),
"azurerm_maps_creator": resourceMapsCreator(),
}
}
Loading