Skip to content

Commit

Permalink
refactor, tests, linting, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jackofallops committed Jun 4, 2020
1 parent c010f75 commit 33b5a7f
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 61 deletions.
1 change: 1 addition & 0 deletions .teamcity/components/generated/services.kt
Expand Up @@ -10,6 +10,7 @@ var services = mapOf(
"authorization" to "Authorization",
"automation" to "Automation",
"batch" to "Batch",
"blueprints" to "Blueprints",
"bot" to "Bot",
"cdn" to "CDN",
"cognitive" to "Cognitive Services",
Expand Down
13 changes: 0 additions & 13 deletions azurerm/internal/services/blueprints/blueprint.go
Expand Up @@ -10,7 +10,6 @@ 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/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/blueprints/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -210,15 +209,3 @@ func flattenArmBlueprintAssignmentResourceGroups(input map[string]*blueprint.Res

return string(b), nil
}

func splitPublishedVersionID(input string) (blueprintID, versionName string) {
versionID, err := parse.VersionID(input)
if err != nil {
return "", ""
}

versionName = versionID.Name
blueprintID = fmt.Sprintf("/%s/providers/Microsoft.Blueprint/blueprints/%s", versionID.Scope, versionID.Blueprint)

return
}
Expand Up @@ -12,6 +12,7 @@ import (
"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/suppress"
"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/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/blueprints/parse"
Expand Down Expand Up @@ -57,20 +58,6 @@ func resourceArmBlueprintAssignment() *schema.Resource {

"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,
//},
//
"version_id": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -148,6 +135,18 @@ func resourceArmBlueprintAssignmentCreateUpdate(d *schema.ResourceData, meta int
blueprintId := d.Get("version_id").(string)
targetScope := d.Get("target_subscription_id").(string)

if d.IsNewResource() {
resp, err := client.Get(ctx, targetScope, name)
if err != nil {
if !utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("failure checking for existing Blueprint Assignment %q in scope %q", name, targetScope)
}
}
if !utils.ResponseWasNotFound(resp.Response) {
return tf.ImportAsExistsError("azurerm_blueprint_assignment", *resp.ID)
}
}

assignment := blueprint.Assignment{
AssignmentProperties: &blueprint.AssignmentProperties{
BlueprintID: utils.String(blueprintId), // This is mislabeled - The ID is that of the Published Version, not just the Blueprint
Expand Down
Expand Up @@ -2,14 +2,14 @@ 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"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
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 @@ -23,6 +23,12 @@ func dataSourceArmBlueprintPublishedVersion() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"blueprint_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"scope_id": {
Type: schema.TypeString,
Required: true,
Expand All @@ -32,26 +38,19 @@ func dataSourceArmBlueprintPublishedVersion() *schema.Resource {
),
},

"blueprint_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

"version": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},

// Computed

"type": {
"description": {
Type: schema.TypeString,
Computed: true,
},

"time_created": {
"display_name": {
Type: schema.TypeString,
Computed: true,
},
Expand All @@ -66,12 +65,12 @@ func dataSourceArmBlueprintPublishedVersion() *schema.Resource {
Computed: true,
},

"display_name": {
"time_created": {
Type: schema.TypeString,
Computed: true,
},

"description": {
"type": {
Type: schema.TypeString,
Computed: true,
},
Expand Down
Expand Up @@ -32,6 +32,28 @@ func TestAccBlueprintAssignment_basic(t *testing.T) {
})
}

func TestAccBlueprintAssignment_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_blueprint_assignment", "test")

resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMBlueprintAssignmentDestroy,
Steps: []resource.TestStep{
{
Config: testAccBlueprintAssignment_basic(data, "testAcc_basicSubscription", "v0.1_testAcc"),
Check: resource.ComposeTestCheckFunc(
testCheckBlueprintAssignmentExists(data.ResourceName),
),
},
{
Config: testAccBlueprintAssignment_requiresImport(data, "testAcc_basicSubscription", "v0.1_testAcc"),
ExpectError: acceptance.RequiresImportError("azurerm_blueprint_assignment"),
},
},
})
}

// Scenario: BP with RG's, locking and parameters/policies stored at Subscription, applied to subscription
func TestAccBlueprintAssignment_subscriptionComplete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_blueprint_assignment", "test")
Expand Down Expand Up @@ -123,8 +145,10 @@ func testCheckAzureRMBlueprintAssignmentDestroy(s *terraform.State) error {
}

func testAccBlueprintAssignment_basic(data acceptance.TestData, bpName string, version string) string {
subscription := data.Client().SubscriptionIDAlt
return fmt.Sprintf(`
provider "azurerm" {
subscription_id = "%s"
features {}
}
Expand Down Expand Up @@ -175,15 +199,18 @@ resource "azurerm_blueprint_assignment" "test" {
azurerm_role_assignment.test
]
}
`, bpName, version, data.RandomInteger, data.RandomInteger, data.Locations.Primary)
`, subscription, bpName, version, data.RandomInteger, data.RandomInteger, data.Locations.Primary)
}

// This test config creates a UM-MSI and assigns Owner to the target subscription. This is necessary due to the changes
// the referenced Blueprint Version needs to make to successfully apply. If the test does not exit cleanly, "dangling"
// resources can include the Role Assignment(s) at the Subscription, which will need to be removed
func testAccBlueprintAssignment_subscriptionComplete(data acceptance.TestData, bpName string, version string) string {
subscription := data.Client().SubscriptionIDAlt

return fmt.Sprintf(`
provider "azurerm" {
subscription_id = "%s"
features {}
}
Expand All @@ -192,7 +219,7 @@ data "azurerm_client_config" "current" {}
data "azurerm_subscription" "test" {}
data "azurerm_blueprint_definition" "test" {
name = "%s"
name = "%s"
scope_id = data.azurerm_subscription.test.id
}
Expand Down Expand Up @@ -267,7 +294,7 @@ resource "azurerm_blueprint_assignment" "test" {
azurerm_role_assignment.owner
]
}
`, bpName, version, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
`, subscription, bpName, version, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func testAccBlueprintAssignment_rootManagementGroup(data acceptance.TestData, bpName string, version string) string {
Expand Down Expand Up @@ -340,3 +367,29 @@ resource "azurerm_blueprint_assignment" "test" {
}
`, bpName, version, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.Locations.Primary)
}

func testAccBlueprintAssignment_requiresImport(data acceptance.TestData, bpName string, version string) string {
template := testAccBlueprintAssignment_basic(data, bpName, version)

return fmt.Sprintf(`
%s
resource "azurerm_blueprint_assignment" "import" {
name = azurerm_blueprint_assignment.test.name
target_subscription_id = azurerm_blueprint_assignment.test.target_subscription_id
version_id = azurerm_blueprint_assignment.test.version_id
location = azurerm_blueprint_assignment.test.location
identity {
type = "UserAssigned"
identity_ids = [azurerm_user_assigned_identity.test.id]
}
depends_on = [
azurerm_role_assignment.test
]
}
`, template)
}
Expand Up @@ -17,7 +17,7 @@ func TestAccDataSourceBlueprintDefinition_basic(t *testing.T) {
Providers: acceptance.SupportedProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceBlueprintDefinition_basic(),
Config: testAccDataSourceBlueprintDefinition_basic(data),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "description", "Acceptance Test stub for Blueprints at Subscription"),
resource.TestCheckResourceAttr(data.ResourceName, "name", "testAcc_basicSubscription"),
Expand Down Expand Up @@ -71,9 +71,11 @@ func TestAccDataSourceBlueprintDefinition_basicAtChildManagementGroup(t *testing
})
}

func testAccDataSourceBlueprintDefinition_basic() string {
return `
func testAccDataSourceBlueprintDefinition_basic(data acceptance.TestData) string {
subscription := data.Client().SubscriptionIDAlt
return fmt.Sprintf(`
provider "azurerm" {
subscription_id = "%s"
features {}
}
Expand All @@ -84,7 +86,7 @@ data "azurerm_blueprint_definition" "test" {
scope_id = data.azurerm_subscription.current.id
}
`
`, subscription)
}

func testAccDataSourceBlueprintDefinition_basicAtManagementGroup(managementGroup string) string {
Expand All @@ -99,7 +101,7 @@ data "azurerm_management_group" "test" {
data "azurerm_blueprint_definition" "test" {
name = "testAcc_staticStubManagementGroup"
scope_id = data.azurerm_management_group.test.id
scope_id = data.azurerm_management_group.test.id
}
`, managementGroup)
Expand Down
Expand Up @@ -17,7 +17,7 @@ func TestAccDataSourceBlueprintPublishedVersion_atSubscription(t *testing.T) {
Providers: acceptance.SupportedProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceBlueprintPublishedVersion_atSubscription("testAcc_basicSubscription", "v0.1_testAcc"),
Config: testAccDataSourceBlueprintPublishedVersion_atSubscription(data, "testAcc_basicSubscription", "v0.1_testAcc"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(data.ResourceName, "target_scope", "subscription"),
resource.TestCheckResourceAttr(data.ResourceName, "description", "Acceptance Test stub for Blueprints at Subscription"),
Expand Down Expand Up @@ -69,9 +69,12 @@ func TestAccDataSourceBlueprintPublishedVersion_atChildManagementGroup(t *testin
})
}

func testAccDataSourceBlueprintPublishedVersion_atSubscription(bpName, version string) string {
func testAccDataSourceBlueprintPublishedVersion_atSubscription(data acceptance.TestData, bpName string, version string) string {
subscription := data.Client().SubscriptionIDAlt

return fmt.Sprintf(`
provider "azurerm" {
subscription_id = "%s"
features {}
}
Expand All @@ -82,7 +85,7 @@ data "azurerm_blueprint_published_version" "test" {
blueprint_name = "%s"
version = "%s"
}
`, bpName, version)
`, subscription, bpName, version)
}

func testAccDataSourceBlueprintPublishedVersion_atRootManagementGroup(bpName, version string) string {
Expand Down
3 changes: 2 additions & 1 deletion azurerm/internal/services/blueprints/validate/blueprint.go
Expand Up @@ -2,8 +2,9 @@ package validate

import (
"fmt"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/blueprints/parse"
"regexp"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/blueprints/parse"
)

func BlueprintID(i interface{}, k string) (warnings []string, errors []error) {
Expand Down
10 changes: 5 additions & 5 deletions website/docs/d/blueprint_definition.html.markdown
Expand Up @@ -21,7 +21,7 @@ data "azurerm_management_group" "root" {
data "azurerm_blueprint_definition" "example" {
name = "exampleManagementGroupBP"
scope_id = data.azurerm_management_group.root.id
scope_id = data.azurerm_management_group.root.id
}
```
Expand All @@ -36,15 +36,15 @@ data "azurerm_blueprint_definition" "example" {

* `id` - The Azure Resource ID of the Blueprint Definition.

* `target_scope` - The target scope.
* `description` - The description of the Blueprint Definition.

* `display_name` - The display name of the Blueprint Definition.

* `description` - The description of the Blueprint Definition.
* `last_modified` - The timestamp of when this last modification was saved to the Blueprint Definition.

* `time_created` - The timestamp of when this Blueprint Definition was created.
* `target_scope` - The target scope.

* `last_modified` - The timestamp of when this last modification was saved to the Blueprint Definition.
* `time_created` - The timestamp of when this Blueprint Definition was created.

* `versions` - A list of versions published for this Blueprint Definition.

Expand Down
4 changes: 2 additions & 2 deletions website/docs/d/blueprint_published_version.html.markdown
Expand Up @@ -24,10 +24,10 @@ data "azurerm_blueprint_published_version" "test" {

## Argument Reference

* `scope_id` - (Required) The Resource ID of the scope where the Blueprint Definition is stored. This will be with either a Subscription ID or Management Group ID.

* `blueprint_name` - (Required) The name of the Blueprint Definition

* `scope_id` - (Required) The Resource ID of the scope where the Blueprint Definition is stored. This will be with either a Subscription ID or Management Group ID.

* `version` - (Required) The Version name of the Published Version of the Blueprint Definition


Expand Down

0 comments on commit 33b5a7f

Please sign in to comment.