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

provider/azurerm: add cdn profile #4740

Merged
merged 1 commit into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
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
Copy link
Contributor

Choose a reason for hiding this comment

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

How did this one come about?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

d'oh!


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{
Copy link
Contributor

Choose a reason for hiding this comment

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

Once the Storage Account work gets merged in I wonder if we can use the polling request here - perhaps not though as it seems to be a state that will update and this might be the more appropriate way.

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