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

analysisservices - add ID parsing function #5356

Merged
merged 4 commits into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"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/analysisservices/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"
)
Expand All @@ -33,9 +35,10 @@ func resourceArmAnalysisServicesServer() *schema.Resource {
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.AnalysisServicesServerID(id)
return err
}),

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -189,26 +192,22 @@ func resourceArmAnalysisServicesServerRead(d *schema.ResourceData, meta interfac
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resourceGroup := id.ResourceGroup
name := id.Path["servers"]

server, err := client.GetDetails(ctx, resourceGroup, name)

server, err := client.GetDetails(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(server.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving Analytics Services Server %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("Error retrieving Analytics Services Server %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)

if location := server.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
Expand All @@ -232,7 +231,6 @@ func resourceArmAnalysisServicesServerRead(d *schema.ResourceData, meta interfac
}

d.Set("querypool_connection_mode", string(serverProps.QuerypoolConnectionMode))

d.Set("server_full_name", serverProps.ServerFullName)

if containerUri, ok := d.GetOk("backup_blob_container_uri"); ok {
Expand All @@ -250,14 +248,11 @@ func resourceArmAnalysisServicesServerUpdate(d *schema.ResourceData, meta interf

log.Printf("[INFO] preparing arguments for Azure ARM Analysis Services Server creation.")

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

resourceGroup := id.ResourceGroup
name := id.Path["servers"]

serverProperties := expandAnalysisServicesServerMutableProperties(d)
sku := d.Get("sku").(string)
t := d.Get("tags").(map[string]interface{})
Expand All @@ -268,13 +263,13 @@ func resourceArmAnalysisServicesServerUpdate(d *schema.ResourceData, meta interf
ServerMutableProperties: serverProperties,
}

future, err := client.Update(ctx, resourceGroup, name, analysisServicesServer)
future, err := client.Update(ctx, id.ResourceGroup, id.Name, analysisServicesServer)
if err != nil {
return fmt.Errorf("Error creating Analysis Services Server %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("Error creating Analysis Services Server %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for completion of Analysis Services Server %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("Error waiting for completion of Analysis Services Server %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

return resourceArmAnalysisServicesServerRead(d, meta)
Expand All @@ -285,21 +280,18 @@ func resourceArmAnalysisServicesServerDelete(d *schema.ResourceData, meta interf
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resGroup := id.ResourceGroup
name := id.Path["servers"]

future, err := client.Delete(ctx, resGroup, name)
future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("Error deleting Analysis Services Server %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("Error deleting Analysis Services Server %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for deletion of Analysis Services Server %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("Error waiting for deletion of Analysis Services Server %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}

return nil
Expand Down
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 AnalysisServicesServerId struct {
ResourceGroup string
Name string
}

func AnalysisServicesServerID(input string) (*AnalysisServicesServerId, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add unit tests for this parser, similar to the other parsers?

id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Analysis Services Server ID %q: %+v", input, err)
}

server := AnalysisServicesServerId{
ResourceGroup: id.ResourceGroup,
}

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

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

return &server, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/analysisservices/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand Down Expand Up @@ -444,10 +445,12 @@ func testCheckAzureRMAnalysisServicesServerDestroy(s *terraform.State) error {
continue
}

name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]
id, err := parse.AnalysisServicesServerID(rs.Primary.ID)
if err != nil {
return err
}

resp, err := client.GetDetails(ctx, resourceGroup, name)
resp, err := client.GetDetails(ctx, id.ResourceGroup, id.Name)

if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
Expand All @@ -472,16 +475,15 @@ func testCheckAzureRMAnalysisServicesServerExists(resourceName string) resource.
return fmt.Errorf("Not found: %s", resourceName)
}

analysisServicesServerName := rs.Primary.Attributes["name"]
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
if !hasResourceGroup {
return fmt.Errorf("Bad: no resource group found in state for Analysis Services Server: %s", analysisServicesServerName)
id, err := parse.AnalysisServicesServerID(rs.Primary.ID)
if err != nil {
return err
}

resp, err := client.GetDetails(ctx, resourceGroup, analysisServicesServerName)
resp, err := client.GetDetails(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Bad: Analysis Services Server %q (resource group: %q) does not exist", analysisServicesServerName, resourceGroup)
return fmt.Errorf("Bad: Analysis Services Server %q (resource group: %q) does not exist", id.Name, id.ResourceGroup)
}

return fmt.Errorf("Bad: Get on analysisServicesServerClient: %+v", err)
Expand Down