Skip to content

Commit

Permalink
Merge pull request #4740 from stack72/azurerm-cdn-profile
Browse files Browse the repository at this point in the history
provider/azurerm: add cdn profile
  • Loading branch information
jen20 committed Jan 19, 2016
2 parents a4abb0e + 183eb24 commit 44700f7
Show file tree
Hide file tree
Showing 7 changed files with 463 additions and 3 deletions.
9 changes: 9 additions & 0 deletions builtin/providers/azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest"
"github.com/Azure/azure-sdk-for-go/Godeps/_workspace/src/github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/azure-sdk-for-go/arm/cdn"
"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
Expand Down Expand Up @@ -40,6 +41,8 @@ type ArmClient struct {
routeTablesClient network.RouteTablesClient
routesClient network.RoutesClient

cdnProfilesClient cdn.ProfilesClient

providers resources.ProvidersClient
resourceGroupClient resources.GroupsClient
tagsClient resources.TagsClient
Expand Down Expand Up @@ -246,5 +249,11 @@ func (c *Config) getArmClient() (*ArmClient, error) {
suc.Sender = autorest.CreateSender(withRequestLogging())
client.storageUsageClient = suc

cpc := cdn.NewProfilesClient(c.SubscriptionID)
setUserAgent(&cpc.Client)
cpc.Authorizer = spt
cpc.Sender = autorest.CreateSender(withRequestLogging())
client.cdnProfilesClient = cpc

return &client, nil
}
3 changes: 2 additions & 1 deletion builtin/providers/azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_network_interface": resourceArmNetworkInterface(),
"azurerm_route_table": resourceArmRouteTable(),
"azurerm_route": resourceArmRoute(),
"azurerm_cdn_profile": resourceArmCdnProfile(),
},
ConfigureFunc: providerConfigure,
}
Expand Down Expand Up @@ -95,7 +96,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
func registerAzureResourceProvidersWithSubscription(config *Config, client *ArmClient) error {
providerClient := client.providers

providers := []string{"Microsoft.Network", "Microsoft.Compute"}
providers := []string{"Microsoft.Network", "Microsoft.Compute", "Microsoft.Cdn"}

for _, v := range providers {
res, err := providerClient.Register(v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func testCheckAzureRMAvailabilitySetExists(name string) resource.TestCheckFunc {
}

func testCheckAzureRMAvailabilitySetDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).vnetClient
conn := testAccProvider.Meta().(*ArmClient).availSetClient

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_availability_set" {
Expand All @@ -129,7 +129,7 @@ func testCheckAzureRMAvailabilitySetDestroy(s *terraform.State) error {
name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]

resp, err := conn.Get(resourceGroup, name, "")
resp, err := conn.Get(resourceGroup, name)

if err != nil {
return nil
Expand Down
186 changes: 186 additions & 0 deletions builtin/providers/azurerm/resource_arm_cdn_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package azurerm

import (
"fmt"
"log"
"net/http"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/arm/cdn"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceArmCdnProfile() *schema.Resource {
return &schema.Resource{
Create: resourceArmCdnProfileCreate,
Read: resourceArmCdnProfileRead,
Update: resourceArmCdnProfileUpdate,
Delete: resourceArmCdnProfileDelete,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"location": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: azureRMNormalizeLocation,
},

"resource_group_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"sku": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateCdnProfileSku,
},

"tags": tagsSchema(),
},
}
}

func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
cdnProfilesClient := client.cdnProfilesClient

log.Printf("[INFO] preparing arguments for Azure ARM CDN Profile creation.")

name := d.Get("name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
sku := d.Get("sku").(string)
tags := d.Get("tags").(map[string]interface{})

properties := cdn.ProfilePropertiesCreateParameters{
Sku: &cdn.Sku{
Name: cdn.SkuName(sku),
},
}

cdnProfile := cdn.ProfileCreateParameters{
Location: &location,
Properties: &properties,
Tags: expandTags(tags),
}

resp, err := cdnProfilesClient.Create(name, cdnProfile, resGroup)
if err != nil {
return err
}

d.SetId(*resp.ID)

log.Printf("[DEBUG] Waiting for CDN Profile (%s) to become available", name)
stateConf := &resource.StateChangeConf{
Pending: []string{"Accepted", "Updating", "Creating"},
Target: "Succeeded",
Refresh: cdnProfileStateRefreshFunc(client, resGroup, name),
Timeout: 10 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for CDN Profile (%s) to become available: %s", name, err)
}

return resourceArmCdnProfileRead(d, meta)
}

func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error {
cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["Profiles"]

resp, err := cdnProfilesClient.Get(name, resGroup)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err)
}

if resp.Properties != nil && resp.Properties.Sku != nil {
d.Set("sku", string(resp.Properties.Sku.Name))
}

flattenAndSetTags(d, resp.Tags)

return nil
}

func resourceArmCdnProfileUpdate(d *schema.ResourceData, meta interface{}) error {
cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient

if !d.HasChange("tags") {
return nil
}

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
newTags := d.Get("tags").(map[string]interface{})

props := cdn.ProfileUpdateParameters{
Tags: expandTags(newTags),
}

_, err := cdnProfilesClient.Update(name, props, resGroup)
if err != nil {
return fmt.Errorf("Error issuing Azure ARM update request to update CDN Profile %q: %s", name, err)
}

return resourceArmCdnProfileRead(d, meta)
}

func resourceArmCdnProfileDelete(d *schema.ResourceData, meta interface{}) error {
cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["Profiles"]

_, err = cdnProfilesClient.DeleteIfExists(name, resGroup)

return err
}

func cdnProfileStateRefreshFunc(client *ArmClient, resourceGroupName string, cdnProfileName string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.cdnProfilesClient.Get(cdnProfileName, resourceGroupName)
if err != nil {
return nil, "", fmt.Errorf("Error issuing read request in cdnProfileStateRefreshFunc to Azure ARM for CND Profile '%s' (RG: '%s'): %s", cdnProfileName, resourceGroupName, err)
}
return res, string(res.Properties.ProvisioningState), nil
}
}

func validateCdnProfileSku(v interface{}, k string) (ws []string, errors []error) {
value := strings.ToLower(v.(string))
skus := map[string]bool{
"standard": true,
"premium": true,
}

if !skus[value] {
errors = append(errors, fmt.Errorf("CDN Profile SKU can only be Standard or Premium"))
}
return
}
Loading

0 comments on commit 44700f7

Please sign in to comment.