-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d'oh!