Skip to content

Commit

Permalink
refactor for IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
jackofallops committed Jun 3, 2020
1 parent c9f0110 commit 5c756f7
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 224 deletions.
Expand Up @@ -3,7 +3,6 @@ package blueprints
import (
"fmt"
"log"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/blueprint/mgmt/2018-11-01-preview/blueprint"
Expand Down Expand Up @@ -43,46 +42,38 @@ func resourceArmBlueprintAssignment() *schema.Resource {
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"scope_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"subscriptions",
"managementGroup",
}, true),
},

"scope": {
"target_subscription_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

"location": location.Schema(),

"identity": ManagedIdentitySchema(),

"blueprint_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.BlueprintID,
},

"version_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotEmpty,
},

//"blueprint_id": {
// Type: schema.TypeString,
// Optional: true,
// Computed: true,
// ValidateFunc: validate.BlueprintID,
//},
//
//"version_name": {
// Type: schema.TypeString,
// Optional: true,
// Computed: true,
// ValidateFunc: validation.StringIsNotEmpty,
//},
//
"version_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Required: true,
ValidateFunc: validate.BlueprintVersionID,
},

Expand Down Expand Up @@ -153,31 +144,9 @@ func resourceArmBlueprintAssignmentCreateUpdate(d *schema.ResourceData, meta int
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

var name, targetScope, blueprintId string

if versionIdRaw, ok := d.GetOk("version_id"); ok {
if _, ok := d.GetOk("blueprint_id"); ok {
return fmt.Errorf("cannot specify `blueprint_id` when `version_id` is specified")
}

if _, ok := d.GetOk("version_name"); ok {
return fmt.Errorf("cannot specify `version_name` when `version_id` is specified")
}
blueprintId = versionIdRaw.(string)
} else {
if bpIDRaw, ok := d.GetOk("blueprint_id"); ok {
if versionName, ok := d.GetOk("version_name"); ok {
blueprintId = fmt.Sprintf("%s/versions/%s", bpIDRaw.(string), versionName.(string))
} else {
return fmt.Errorf("`version_name` must be specified if `version_id` is not supplied")
}
} else {
return fmt.Errorf("`blueprint_id` must be specified if `version_id` is not supplied")
}
}

targetScope = fmt.Sprintf("%s/%s", d.Get("scope_type"), d.Get("scope"))
name = d.Get("name").(string)
name := d.Get("name").(string)
blueprintId := d.Get("version_id").(string)
targetScope := d.Get("target_subscription_id").(string)

assignment := blueprint.Assignment{
AssignmentProperties: &blueprint.AssignmentProperties{
Expand Down Expand Up @@ -237,7 +206,7 @@ func resourceArmBlueprintAssignmentCreateUpdate(d *schema.ResourceData, meta int
}

if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Failed waiting for Blueprint Assignment %q (Scope %q): %+v", name, targetScope, err)
return fmt.Errorf("failed waiting for Blueprint Assignment %q (Scope %q): %+v", name, targetScope, err)
}

d.SetId(*resp.ID)
Expand Down Expand Up @@ -274,13 +243,7 @@ func resourceArmBlueprintAssignmentRead(d *schema.ResourceData, meta interface{}
}

if resp.Scope != nil {
scopeParts := strings.Split(strings.Trim(*resp.Scope, "/"), "/")
if len(scopeParts) == 2 {
d.Set("scope_type", scopeParts[0])
d.Set("scope", scopeParts[1])
} else {
return fmt.Errorf("read on Assignment scope failed, got: %+v", *resp.Scope)
}
d.Set("target_subscription_id", resp.Scope)
}

if resp.Location != nil {
Expand All @@ -294,9 +257,6 @@ func resourceArmBlueprintAssignmentRead(d *schema.ResourceData, meta interface{}
if resp.AssignmentProperties != nil {
if resp.AssignmentProperties.BlueprintID != nil {
d.Set("version_id", resp.AssignmentProperties.BlueprintID)
bpID, versionName := splitPublishedVersionID(*resp.BlueprintID)
d.Set("blueprint_id", bpID)
d.Set("version_name", versionName)
}

if resp.Parameters != nil {
Expand Down
Expand Up @@ -6,8 +6,10 @@ import (

"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/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/blueprints/validate"
mgValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/managementgroup/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -27,19 +29,13 @@ func dataSourceArmBlueprintDefinition() *schema.Resource {
ValidateFunc: validate.BlueprintName,
},

"scope_type": {
"scope_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"subscriptions",
"managementGroup",
}, false),
},

"scope_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
ValidateFunc: validation.Any(
azure.ValidateResourceID,
mgValidate.ManagementGroupID,
),
},

// Computed
Expand Down Expand Up @@ -85,15 +81,7 @@ func dataSourceArmBlueprintDefinitionRead(d *schema.ResourceData, meta interface
defer cancel()

name := d.Get("name").(string)
var scope string
scopeName := d.Get("scope_name").(string)
scopeType := d.Get("scope_type").(string)
switch scopeType {
case "subscriptions":
scope = fmt.Sprintf("subscriptions/%s", scopeName)
case "managementGroup":
scope = fmt.Sprintf("providers/Microsoft.Management/managementGroups/%s", scopeName)
}
scope := d.Get("scope_id").(string)

resp, err := client.Get(ctx, scope, name)
if err != nil {
Expand Down Expand Up @@ -130,3 +118,4 @@ func dataSourceArmBlueprintDefinitionRead(d *schema.ResourceData, meta interface

return nil
}

Expand Up @@ -2,6 +2,9 @@ package blueprints

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
mgValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/managementgroup/validate"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand All @@ -20,16 +23,13 @@ func dataSourceArmBlueprintPublishedVersion() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"subscription_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.UUID,
},

"management_group": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.NoEmptyStrings,
"scope_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.Any(
azure.ValidateResourceID,
mgValidate.ManagementGroupID,
),
},

"blueprint_name": {
Expand Down Expand Up @@ -84,28 +84,7 @@ func dataSourceArmBlueprintPublishedVersionRead(d *schema.ResourceData, meta int
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

subscriptionRaw := d.Get("subscription_id")
var subscription, managementGroup string
if subscriptionRaw != nil {
subscription = subscriptionRaw.(string)
}
managementGroupRaw := d.Get("management_group")
if managementGroupRaw != nil {
managementGroup = managementGroupRaw.(string)
}

var scope string

if subscription == "" && managementGroup == "" {
return fmt.Errorf("One of subscription or management group must be specified")
}

if subscription != "" {
scope = fmt.Sprintf("subscriptions/%s", subscription)
} else {
scope = fmt.Sprintf("providers/Microsoft.Management/managementGroups/%s", managementGroup)
}

scope := d.Get("scope_id").(string)
blueprintName := d.Get("blueprint_name").(string)
versionID := d.Get("version").(string)

Expand All @@ -124,7 +103,7 @@ func dataSourceArmBlueprintPublishedVersionRead(d *schema.ResourceData, meta int
d.SetId(*resp.ID)
}

if resp.Type == nil {
if resp.Type != nil {
d.Set("type", resp.Type)
}

Expand Down
Expand Up @@ -45,7 +45,7 @@ func DefinitionID(input string) (*DefinitionId, error) {
case "subscriptions":
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Image ID %q: %+v", input, err)
return nil, fmt.Errorf("[ERROR] Unable to parse Blueprint Definition ID %q: %+v", input, err)
}

definitionId.Scope = fmt.Sprintf("subscriptions/%s", id.SubscriptionID)
Expand Down

0 comments on commit 5c756f7

Please sign in to comment.