-
Notifications
You must be signed in to change notification settings - Fork 4.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
Create azure App Service resource #2
Closed
lstolyarov
wants to merge
20
commits into
hashicorp:master
from
lstolyarov:azurerm/app-service-resource
Closed
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
235512b
Adding azure app service resource
leostolyarov a845254
adding importer
leostolyarov 9844930
adding some tests
leostolyarov 195ddb5
Merge branch 'master' into azurerm/app-service-resource
lstolyarov 0e8d14c
updating tests to use testlocation()
leostolyarov 41b9e88
setting tags
leostolyarov 30ce964
Resolve App Service Import Acceptance test failure
02eb048
removed additional spaces which were inserted by editor
e6ec119
Merge pull request #2 from maniSbindra/app-service-resource
lstolyarov 9d1fb3e
Merge branch 'master' into azurerm/app-service-resource
lstolyarov 119f353
removing duplicates
leostolyarov 81b02a5
adding documentation
leostolyarov ed5a0bf
running fmt
leostolyarov 987a8a2
updating app service resource
leostolyarov 7440394
updating app service code
leostolyarov 5cd174c
updating tests
leostolyarov 1d2a410
updating documentation
leostolyarov ae50429
setting site config
leostolyarov b579345
adding more tests
leostolyarov e97eb41
making use of utils module
leostolyarov 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package azurerm | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAzureRMAppService_importBasic(t *testing.T) { | ||
resourceName := "azurerm_app_service.test" | ||
|
||
ri := acctest.RandInt() | ||
config := testAccAzureRMAppService_basic(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMAppServiceDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: config, | ||
}, | ||
|
||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"force_dns_registration", "skip_custom_domain_verification", "skip_dns_registration", "delete_metrics"}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMAppService_importComplete(t *testing.T) { | ||
resourceName := "azurerm_app_service.test" | ||
|
||
ri := acctest.RandInt() | ||
config := testAccAzureRMAppService_complete(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMAppServiceDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: config, | ||
}, | ||
|
||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"force_dns_registration", "skip_custom_domain_verification", "skip_dns_registration", "delete_metrics"}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMAppService_importCompleteAlwaysOn(t *testing.T) { | ||
resourceName := "azurerm_app_service.test" | ||
|
||
ri := acctest.RandInt() | ||
config := testAccAzureRMAppService_completeAlwaysOn(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMAppServiceDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: config, | ||
}, | ||
|
||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIgnore: []string{"force_dns_registration", "skip_custom_domain_verification", "skip_dns_registration", "delete_metrics"}, | ||
}, | ||
}, | ||
}) | ||
} |
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,223 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/web" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmAppService() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmAppServiceCreateUpdate, | ||
Read: resourceArmAppServiceRead, | ||
Update: resourceArmAppServiceCreateUpdate, | ||
Delete: resourceArmAppServiceDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
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.TypeInt, | ||
Optional: true, | ||
}, | ||
"site_config": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"app_service_plan_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"always_on": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"location": locationSchema(), | ||
"tags": tagsSchema(), | ||
"delete_metrics": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmAppServiceCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
appClient := meta.(*ArmClient).appsClient | ||
|
||
log.Printf("[INFO] preparing arguments for Azure ARM App Service creation.") | ||
|
||
resGroup := d.Get("resource_group_name").(string) | ||
name := d.Get("name").(string) | ||
skipDNSRegistration := d.Get("skip_dns_registration").(bool) | ||
skipCustomDomainVerification := d.Get("skip_custom_domain_verification").(bool) | ||
forceDNSRegistration := d.Get("force_dns_registration").(bool) | ||
ttlInSeconds := 0 | ||
if v, ok := d.GetOk("ttl_in_seconds"); ok { | ||
ttlInSeconds = v.(int) | ||
} | ||
location := d.Get("location").(string) | ||
tags := d.Get("tags").(map[string]interface{}) | ||
|
||
siteProps := expandAzureRmAppServiceSiteProps(d) | ||
|
||
siteEnvelope := web.Site{ | ||
Location: &location, | ||
Tags: expandTags(tags), | ||
SiteProperties: siteProps, | ||
} | ||
|
||
_, error := appClient.CreateOrUpdate(resGroup, name, siteEnvelope, &skipDNSRegistration, &skipCustomDomainVerification, &forceDNSRegistration, strconv.Itoa(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: %+v", name, err) | ||
} | ||
|
||
d.Set("name", name) | ||
d.Set("resource_group_name", resGroup) | ||
d.Set("location", azureRMNormalizeLocation(*resp.Location)) | ||
|
||
if siteProps := resp.SiteProperties; siteProps != nil { | ||
d.Set("site_config", flattenAzureRmAppServiceSiteProps(siteProps)) | ||
} | ||
|
||
flattenAndSetTags(d, resp.Tags) | ||
|
||
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 := d.Get("delete_metrics").(bool) | ||
deleteEmptyServerFarm := true | ||
skipDNSRegistration := d.Get("skip_dns_registration").(bool) | ||
|
||
_, err = appClient.Delete(resGroup, name, &deleteMetrics, &deleteEmptyServerFarm, &skipDNSRegistration) | ||
|
||
return err | ||
} | ||
|
||
func expandAzureRmAppServiceSiteProps(d *schema.ResourceData) *web.SiteProperties { | ||
configs := d.Get("site_config").([]interface{}) | ||
siteProps := web.SiteProperties{} | ||
if len(configs) == 0 { | ||
return &siteProps | ||
} | ||
config := configs[0].(map[string]interface{}) | ||
|
||
siteConfig := web.SiteConfig{} | ||
alwaysOn := config["always_on"].(bool) | ||
siteConfig.AlwaysOn = utils.Bool(alwaysOn) | ||
|
||
siteProps.SiteConfig = &siteConfig | ||
|
||
serverFarmID := config["app_service_plan_id"].(string) | ||
siteProps.ServerFarmID = &serverFarmID | ||
|
||
return &siteProps | ||
} | ||
|
||
func flattenAzureRmAppServiceSiteProps(siteProps *web.SiteProperties) []interface{} { | ||
result := make([]interface{}, 0, 1) | ||
site_config := make(map[string]interface{}, 0) | ||
|
||
if siteProps.ServerFarmID != nil { | ||
site_config["app_service_plan_id"] = *siteProps.ServerFarmID | ||
} | ||
|
||
siteConfig := siteProps.SiteConfig | ||
log.Printf("[DEBUG] SiteConfig is %s", siteConfig) | ||
if siteConfig != nil { | ||
if siteConfig.AlwaysOn != nil { | ||
site_config["always_on"] = *siteConfig.AlwaysOn | ||
} | ||
} | ||
|
||
result = append(result, site_config) | ||
return result | ||
} |
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.
can we set the additional fields here?