-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Create azure App Service resource #2
Changes from 1 commit
235512b
a845254
9844930
195ddb5
0e8d14c
41b9e88
30ce964
02eb048
e6ec119
9d1fb3e
119f353
81b02a5
ed5a0bf
987a8a2
7440394
5cd174c
1d2a410
ae50429
b579345
e97eb41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/web" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceArmAppService() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmAppServiceCreateUpdate, | ||
Read: resourceArmAppServiceRead, | ||
Update: resourceArmAppServiceCreateUpdate, | ||
Delete: resourceArmAppServiceDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"resource_group_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"skip_dns_registration": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: true, | ||
}, | ||
"skip_custom_domain_verification": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: true, | ||
}, | ||
"force_dns_registration": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
"ttl_in_seconds": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "", | ||
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. I think we want to remove this line, we can instead then do:
|
||
}, | ||
"app_service_plan_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
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. does this need to be |
||
}, | ||
"always_on": { | ||
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. given there's a ton of possible options here, it'd be nice to pull this out into a nested structure:
|
||
Type: schema.TypeBool, | ||
Optional: true, | ||
}, | ||
"location": locationSchema(), | ||
"tags": tagsSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmAppServiceCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient) | ||
appClient := client.appsClient | ||
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. minor: we'd generally combine these into one variable rather than splitting it out |
||
|
||
log.Printf("[INFO] preparing arguments for Azure ARM Web App creation.") | ||
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. can we rename |
||
|
||
resGroup := d.Get("resource_group_name").(string) | ||
name := d.Get("name").(string) | ||
location := d.Get("location").(string) | ||
skipDNSRegistration := d.Get("skip_dns_registration").(bool) | ||
skipCustomDomainVerification := d.Get("skip_custom_domain_verification").(bool) | ||
forceDNSRegistration := d.Get("force_dns_registration").(bool) | ||
ttlInSeconds := d.Get("ttl_in_seconds").(string) | ||
tags := d.Get("tags").(map[string]interface{}) | ||
|
||
siteConfig := web.SiteConfig{} | ||
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. Looking at the available fields, it feels like it might be worth splitting this out into a separate block? |
||
if v, ok := d.GetOk("always_on"); ok { | ||
alwaysOn := v.(bool) | ||
siteConfig.AlwaysOn = &alwaysOn | ||
} | ||
|
||
siteProps := web.SiteProperties{ | ||
SiteConfig: &siteConfig, | ||
} | ||
if v, ok := d.GetOk("app_service_plan_id"); ok { | ||
serverFarmID := v.(string) | ||
siteProps.ServerFarmID = &serverFarmID | ||
} | ||
|
||
siteEnvelope := web.Site{ | ||
Location: &location, | ||
Tags: expandTags(tags), | ||
SiteProperties: &siteProps, | ||
} | ||
|
||
_, error := appClient.CreateOrUpdate(resGroup, name, siteEnvelope, &skipDNSRegistration, &skipCustomDomainVerification, &forceDNSRegistration, ttlInSeconds, make(chan struct{})) | ||
err := <-error | ||
if err != nil { | ||
return err | ||
} | ||
|
||
read, err := appClient.Get(resGroup, name) | ||
if err != nil { | ||
return err | ||
} | ||
if read.ID == nil { | ||
return fmt.Errorf("Cannot read App Service %s (resource group %s) ID", name, resGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmAppServiceRead(d, meta) | ||
} | ||
|
||
func resourceArmAppServiceRead(d *schema.ResourceData, meta interface{}) error { | ||
appClient := meta.(*ArmClient).appsClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Reading App Service details %s", id) | ||
|
||
resGroup := id.ResourceGroup | ||
name := id.Path["sites"] | ||
|
||
resp, err := appClient.Get(resGroup, name) | ||
if err != nil { | ||
if resp.StatusCode == http.StatusNotFound { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error making Read request on AzureRM App Service %s: %s", name, err) | ||
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. This needs to be
|
||
} | ||
|
||
d.Set("name", name) | ||
d.Set("resource_group_name", resGroup) | ||
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. could we set the other fields here too? |
||
|
||
return nil | ||
} | ||
|
||
func resourceArmAppServiceDelete(d *schema.ResourceData, meta interface{}) error { | ||
appClient := meta.(*ArmClient).appsClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
name := id.Path["sites"] | ||
|
||
log.Printf("[DEBUG] Deleting App Service %s: %s", resGroup, name) | ||
|
||
deleteMetrics := true | ||
deleteEmptyServerFarm := true | ||
skipDNSRegistration := true | ||
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. we can pull the user specified values from (although, FWIW I think we shouldn't be giving the option to delete an empty server farm, since that's managed as a separate resource?) |
||
|
||
_, err = appClient.Delete(resGroup, name, &deleteMetrics, &deleteEmptyServerFarm, &skipDNSRegistration) | ||
|
||
return err | ||
} |
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.
Would we be better to have this as an Int we cast to/from a String?