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_spatial_anchors_account #6011

Merged
merged 5 commits into from
Mar 6, 2020
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
3 changes: 3 additions & 0 deletions azurerm/internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
maps "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/maps/client"
mariadb "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mariadb/client"
media "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/media/client"
mixedreality "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mixedreality/client"
monitor "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/monitor/client"
msi "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/client"
mssql "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mssql/client"
Expand Down Expand Up @@ -108,6 +109,7 @@ type Client struct {
Maps *maps.Client
MariaDB *mariadb.Client
Media *media.Client
MixedReality *mixedreality.Client
Monitor *monitor.Client
MSI *msi.Client
MSSQL *mssql.Client
Expand Down Expand Up @@ -176,6 +178,7 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
client.Maps = maps.NewClient(o)
client.MariaDB = mariadb.NewClient(o)
client.Media = media.NewClient(o)
client.MixedReality = mixedreality.NewClient(o)
client.Monitor = monitor.NewClient(o)
client.MSI = msi.NewClient(o)
client.MSSQL = mssql.NewClient(o)
Expand Down
2 changes: 2 additions & 0 deletions azurerm/internal/provider/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/maps"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mariadb"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/media"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mixedreality"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/monitor"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mssql"
Expand Down Expand Up @@ -102,6 +103,7 @@ func SupportedServices() []common.ServiceRegistration {
maps.Registration{},
mariadb.Registration{},
media.Registration{},
mixedreality.Registration{},
monitor.Registration{},
msi.Registration{},
mssql.Registration{},
Expand Down
19 changes: 19 additions & 0 deletions azurerm/internal/services/mixedreality/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/preview/mixedreality/mgmt/2019-02-28/mixedreality"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

type Client struct {
SpatialAnchorsAccountClient *mixedreality.SpatialAnchorsAccountsClient
}

func NewClient(o *common.ClientOptions) *Client {
SpatialAnchorsAccountClient := mixedreality.NewSpatialAnchorsAccountsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&SpatialAnchorsAccountClient.Client, o.ResourceManagerAuthorizer)

return &Client{
SpatialAnchorsAccountClient: &SpatialAnchorsAccountClient,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type SpatialAnchorsAccountId struct {
ResourceGroup string
Name string
}

func SpatialAnchorsAccountID(input string) (*SpatialAnchorsAccountId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Search Service ID %q: %+v", input, err)
}

service := SpatialAnchorsAccountId{
ResourceGroup: id.ResourceGroup,
}

if service.Name, err = id.PopSegment("spatialAnchorsAccounts"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &service, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package parse

import (
"testing"
)

func TestSearchServiceId(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *SpatialAnchorsAccountId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "Missing Search Services Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MixedReality/spatialAnchorsAccounts/",
Expected: nil,
},
{
Name: "Search Service ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MixedReality/spatialAnchorsAccounts/Account1",
Expected: &SpatialAnchorsAccountId{
Name: "Account1",
ResourceGroup: "resGroup1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MixedReality/SpatialAnchorsAccounts/Service1",
Expected: nil,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := SpatialAnchorsAccountID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
31 changes: 31 additions & 0 deletions azurerm/internal/services/mixedreality/registration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mixedreality

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

type Registration struct{}

// Name is the name of this Service
func (r Registration) Name() string {
return "Mixed Reality"
}

// WebsiteCategories returns a list of categories which can be used for the sidebar
func (r Registration) WebsiteCategories() []string {
return []string{
"Mixed Reality",
}
}

// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{}
}

// SupportedResources returns the supported Resources supported by this Service
func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_spatial_anchors_account": resourceArmSpatialAnchorsAccount(),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package mixedreality

import (
"fmt"
"regexp"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/mixedreality/mgmt/2019-02-28/mixedreality"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mixedreality/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmSpatialAnchorsAccount() *schema.Resource {
return &schema.Resource{
Create: resourceArmSpatialAnchorsAccountCreate,
Read: resourceArmSpatialAnchorsAccountRead,
Delete: resourceArmSpatialAnchorsAccountDelete,
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.SpatialAnchorsAccountID(id)
return err
}),

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

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[-\w\._\(\)]+$`),
"Spatial Anchors Account name must be 1 - 90 characters long, contain only word characters and underscores.",
),
},

"location": azure.SchemaLocation(),

"resource_group_name": azure.SchemaResourceGroupName(),

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

func resourceArmSpatialAnchorsAccountCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MixedReality.SpatialAnchorsAccountClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
location := azure.NormalizeLocation(d.Get("location").(string))
resourceGroup := d.Get("resource_group_name").(string)
t := d.Get("tags").(map[string]interface{})

if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Spatial Anchors Account %q (Resource Group %q): %s", name, resourceGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_spatial_anchors_account", *existing.ID)
}
}

account := mixedreality.SpatialAnchorsAccount{
Location: &location,
Tags: tags.Expand(t),
}

if _, err := client.Create(ctx, resourceGroup, name, account); err != nil {
return fmt.Errorf("creating/updating Spatial Anchors Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("retrieving Spatial Anchors Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if resp.ID == nil {
return fmt.Errorf("cannot read Spatial Anchors Account %q (Resource Group %q) ID", name, resourceGroup)
}

d.SetId(*resp.ID)

return resourceArmSpatialAnchorsAccountRead(d, meta)
}

func resourceArmSpatialAnchorsAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MixedReality.SpatialAnchorsAccountClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("retrieving Spatial Anchors Account %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

return tags.FlattenAndSet(d, resp.Tags)
}

func resourceArmSpatialAnchorsAccountDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MixedReality.SpatialAnchorsAccountClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

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

response, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(response) {
return fmt.Errorf("deleting Spatial Anchors Account %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
}

return nil
}
Loading