-
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: Create azure App Service Plan resource #13634
Closed
lstolyarov
wants to merge
11
commits into
hashicorp:master
from
lstolyarov:azurerm/app-service-plan-resource
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ab002c4
adding app service changes
leostolyarov bddfc79
setting default value for skip_dns_registration
leostolyarov be430ba
Merge branch 'master' into azurerm/app-service-plan-resource
leostolyarov 9f6543b
removing app service resource code
leostolyarov 233f5d5
updated logging messages
leostolyarov c550ee3
initial commit of the acceptance tests for the app service plan
leostolyarov 8539ba3
updated imports for the app service plan acctest
leostolyarov 2055267
Merge branch 'master' into azurerm/app-service-plan-resource
lstolyarov 6a6e1e0
resolved conlicts
leostolyarov 4af4c7c
moved the acctest file to the correct folder
leostolyarov e0a5f0c
Merge remote-tracking branch 'upstream/master' into azurerm/app-servi…
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
132 changes: 132 additions & 0 deletions
132
builtin/providers/azurerm/resource_arm_app_service_plan.go
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,132 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/web" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceArmAppServicePlan() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmAppServicePlanCreateUpdate, | ||
Read: resourceArmAppServicePlanRead, | ||
Update: resourceArmAppServicePlanCreateUpdate, | ||
Delete: resourceArmAppServicePlanDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"resource_group_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"location": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"tier": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"maximum_number_of_workers": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmAppServicePlanCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient) | ||
AppServicePlanClient := client.appServicePlansClient | ||
|
||
log.Printf("[INFO] preparing arguments for Azure App Service Plan creation.") | ||
|
||
resGroup := d.Get("resource_group_name").(string) | ||
name := d.Get("name").(string) | ||
location := d.Get("location").(string) | ||
tier := d.Get("tier").(string) | ||
|
||
sku := web.SkuDescription{ | ||
Name: &tier, | ||
} | ||
|
||
properties := web.AppServicePlanProperties{} | ||
if v, ok := d.GetOk("maximum_number_of_workers"); ok { | ||
maximumNumberOfWorkers := v.(int32) | ||
properties.MaximumNumberOfWorkers = &maximumNumberOfWorkers | ||
} | ||
|
||
appServicePlan := web.AppServicePlan{ | ||
Location: &location, | ||
AppServicePlanProperties: &properties, | ||
Sku: &sku, | ||
} | ||
|
||
_, err := AppServicePlanClient.CreateOrUpdate(resGroup, name, appServicePlan, make(chan struct{})) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
read, err := AppServicePlanClient.Get(resGroup, name) | ||
if err != nil { | ||
return err | ||
} | ||
if read.ID == nil { | ||
return fmt.Errorf("Cannot read Azure App Service Plan %s (resource group %s) ID", name, resGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmAppServicePlanRead(d, meta) | ||
} | ||
|
||
func resourceArmAppServicePlanRead(d *schema.ResourceData, meta interface{}) error { | ||
AppServicePlanClient := meta.(*ArmClient).appServicePlansClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Reading app service plan %s", id) | ||
|
||
resGroup := id.ResourceGroup | ||
name := id.Path["serverfarms"] | ||
|
||
resp, err := AppServicePlanClient.Get(resGroup, name) | ||
if err != nil { | ||
if resp.StatusCode == http.StatusNotFound { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error making Read request on Azure App Service Plan %s: %s", name, err) | ||
} | ||
|
||
d.Set("name", name) | ||
d.Set("resource_group_name", resGroup) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmAppServicePlanDelete(d *schema.ResourceData, meta interface{}) error { | ||
AppServicePlanClient := meta.(*ArmClient).appServicePlansClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
name := id.Path["serverfarms"] | ||
|
||
log.Printf("[DEBUG] Deleting app service plan %s: %s", resGroup, name) | ||
|
||
_, err = AppServicePlanClient.Delete(resGroup, name) | ||
|
||
return err | ||
} |
100 changes: 100 additions & 0 deletions
100
builtin/providers/azurerm/resource_arm_app_service_plan_test.go
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,100 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAzureRMAppServicePlan_standard(t *testing.T) { | ||
ri := acctest.RandInt() | ||
config := fmt.Sprintf(TestAccAzureRMAppServicePlan_standard, ri, ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMAppServicePlanDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMAppServicePlanExists("azurerm_app_service_plan.test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAzureRMAppServicePlanDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*ArmClient).appServicePlansClient | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "azurerm_app_service_plan" { | ||
continue | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
resourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
|
||
resp, err := conn.Get(resourceGroup, name) | ||
|
||
if err != nil { | ||
return nil | ||
} | ||
|
||
if resp.StatusCode != http.StatusNotFound { | ||
return fmt.Errorf("App Service Plan still exists:\n%#v", resp) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testCheckAzureRMAppServicePlanExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
// Ensure we have enough information in state to look up in API | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
appServicePlanName := rs.Primary.Attributes["name"] | ||
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
if !hasResourceGroup { | ||
return fmt.Errorf("Bad: no resource group found in state for App Service Plan: %s", redisName) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*ArmClient).appServicePlansClient | ||
|
||
resp, err := conn.Get(resourceGroup, appServicePlanName) | ||
if err != nil { | ||
return fmt.Errorf("Bad: Get on appServicePlansClient: %s", err) | ||
} | ||
|
||
if resp.StatusCode == http.StatusNotFound { | ||
return fmt.Errorf("Bad: App Service Plan %q (resource group: %q) does not exist", appServicePlanName, resourceGroup) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
var testAccAzureRMAppServicePlan_standard = ` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
location = "West US" | ||
} | ||
resource "azurerm_app_service_plan" "test" { | ||
name = "acctestAppServicePlan-%d" | ||
location = "${azurerm_resource_group.test.location}" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
tier = "S0" | ||
tags { | ||
environment = "production" | ||
} | ||
} | ||
` |
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.
It'd be good to also specify the Size and the Tier here too, as we do in the other resources? We tend to make
sku
a hash like so: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.
@tombuildsstuff do you mean that I have to pass in the Size and Tier as inputs to the provider? At the moment I am only passing in the service tier, e.g. "S0" which determines the tier.