Skip to content

Commit

Permalink
Adds new resource and datasource for App Serice Environment (v2 suppo…
Browse files Browse the repository at this point in the history
…rt only)

Replaces #4956

Co-authored-by: @justinbarias
  • Loading branch information
jackofallops authored and tombuildsstuff committed Feb 19, 2020
1 parent ec44fe6 commit e4f7b02
Show file tree
Hide file tree
Showing 30 changed files with 75,896 additions and 12 deletions.
54 changes: 54 additions & 0 deletions azurerm/internal/services/network/subnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package network

import (
"fmt"

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

type SubnetID struct {
ResourceGroup string
VirtualNetworkName string
Name string
}

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

subnet := SubnetID{
ResourceGroup: id.ResourceGroup,
}

if subnet.VirtualNetworkName, err = id.PopSegment("virtualNetworks"); err != nil {
return nil, err
}

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

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

return &subnet, nil
}

// ValidateSubnetID validates that the specified ID is a valid App Service ID
func ValidateSubnetID(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

if _, err := ParseSubnetID(v); err != nil {
errors = append(errors, fmt.Errorf("Can not parse %q as a resource id: %v", k, err))
return
}

return warnings, errors
}
49 changes: 49 additions & 0 deletions azurerm/internal/services/network/virtual_network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package network

import (
"fmt"

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

type VirtualNetworkID struct {
ResourceGroup string
Name string
}

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

vnet := VirtualNetworkID{
ResourceGroup: id.ResourceGroup,
}

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

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

return &vnet, nil
}

// ValidateVirtualNetworkID validates that the specified ID is a valid App Service ID
func ValidateVirtualNetworkID(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

if _, err := ParseVirtualNetworkID(v); err != nil {
errors = append(errors, fmt.Errorf("Can not parse %q as a resource id: %v", k, err))
return
}

return warnings, errors
}
74 changes: 74 additions & 0 deletions azurerm/internal/services/web/app_service_environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package web

import (
"fmt"
"regexp"

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

type AppServiceEnvironmentResourceID struct {
ResourceGroup string
Name string
}

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

appServiceEnvironment := AppServiceEnvironmentResourceID{
ResourceGroup: id.ResourceGroup,
}

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

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

return &appServiceEnvironment, nil
}

// ValidateAppServiceID validates that the specified ID is a valid App Service ID
func ValidateAppServiceEnvironmentID(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

if _, err := ParseAppServiceEnvironmentID(v); err != nil {
errors = append(errors, fmt.Errorf("Can not parse %q as a resource id: %v", k, err))
return
}

return warnings, errors
}

func validateAppServiceEnvironmentName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)

if matched := regexp.MustCompile(`^[0-9a-zA-Z][-0-9a-zA-Z]{0,61}[0-9a-zA-Z]$`).Match([]byte(value)); !matched {
errors = append(errors, fmt.Errorf("%q may only contain alphanumeric characters and dashes up to 60 characters in length, and must start and end in an alphanumeric", k))
}

return warnings, errors
}

func validateAppServiceEnvironmentPricingTier(v interface{}, k string) (warnings []string, errors []error) {
tier := v.(string)

valid := []string{"I1", "I2", "I3"}

for _, val := range valid {
if val == tier {
return
}
}
errors = append(errors, fmt.Errorf("pricing_tier must be one of %q", valid))
return warnings, errors
}
103 changes: 103 additions & 0 deletions azurerm/internal/services/web/app_service_environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package web

import "testing"

func TestParseAppServiceEnvironmentID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *AppServiceEnvironmentResourceID
}{
{
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 environment name value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/providers/Microsoft.Web/hostingEnvironments/",
Expected: nil,
},
{
Name: "Valid",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup1/providers/Microsoft.Web/hostingEnvironments/TestASEv2",
Expected: &AppServiceEnvironmentResourceID{
ResourceGroup: "testGroup1",
Name: "TestASEv2",
},
},
}

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

actual, err := ParseAppServiceEnvironmentID(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)
}
}
}

func TestValidateAppServiceEnvironmentID(t *testing.T) {
cases := []struct {
ID string
Valid bool
}{
{
ID: "",
Valid: false,
},
{
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Valid: false,
},
{
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/",
Valid: false,
},
{
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/providers/Microsoft.Web/hostingEnvironments/",
Valid: false,
},
{
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testGroup1/providers/Microsoft.Web/hostingEnvironments/TestASEv2",
Valid: true,
},
}
for _, tc := range cases {
t.Logf("[DEBUG] Testing Value %s", tc.ID)
_, errors := ValidateAppServiceEnvironmentID(tc.ID, "test")
valid := len(errors) == 0

if tc.Valid != valid {
t.Fatalf("Expected %t but got %t", tc.Valid, valid)
}
}
}
26 changes: 16 additions & 10 deletions azurerm/internal/services/web/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ package client

import (
"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2018-02-01/web"
asev2 "github.com/Azure/azure-sdk-for-go/services/web/mgmt/2019-08-01/web"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

type Client struct {
AppServicePlansClient *web.AppServicePlansClient
AppServicesClient *web.AppsClient
BaseClient *web.BaseClient
CertificatesClient *web.CertificatesClient
CertificatesOrderClient *web.AppServiceCertificateOrdersClient
AppServiceEnvironmentsClient *asev2.AppServiceEnvironmentsClient
AppServicePlansClient *web.AppServicePlansClient
AppServicesClient *web.AppsClient
BaseClient *web.BaseClient
CertificatesClient *web.CertificatesClient
CertificatesOrderClient *web.AppServiceCertificateOrdersClient
}

func NewClient(o *common.ClientOptions) *Client {
appServiceEnvironmentsClient := asev2.NewAppServiceEnvironmentsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&appServiceEnvironmentsClient.Client, o.ResourceManagerAuthorizer)

appServicePlansClient := web.NewAppServicePlansClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&appServicePlansClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -30,10 +35,11 @@ func NewClient(o *common.ClientOptions) *Client {
o.ConfigureClient(&certificatesOrderClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AppServicePlansClient: &appServicePlansClient,
AppServicesClient: &appServicesClient,
BaseClient: &baseClient,
CertificatesClient: &certificatesClient,
CertificatesOrderClient: &certificatesOrderClient,
AppServiceEnvironmentsClient: &appServiceEnvironmentsClient,
AppServicePlansClient: &appServicePlansClient,
AppServicesClient: &appServicesClient,
BaseClient: &baseClient,
CertificatesClient: &certificatesClient,
CertificatesOrderClient: &certificatesOrderClient,
}
}
Loading

0 comments on commit e4f7b02

Please sign in to comment.