-
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
Creating azure app insights resource #3
Changes from 5 commits
bd7b44b
3bf8958
a1bfccd
c2fac3e
36d1680
f814fe2
ce4c4d9
6860ffe
39f4b66
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,34 @@ | ||
package azurerm | ||
|
||
import ( | ||
"testing" | ||
|
||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAzureRMAppInsights_importBasic(t *testing.T) { | ||
resourceName := "azurerm_application_insights.test" | ||
|
||
ri := acctest.RandInt() | ||
config := fmt.Sprintf(testAccAzureRMApplicationInsights_basic, ri, ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMAppInsightsDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
|
||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/appinsights" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
) | ||
|
||
func resourceArmApplicationInsights() *schema.Resource { | ||
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 add some documentation for this resource too? 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. (done) |
||
return &schema.Resource{ | ||
Create: resourceArmApplicationInsightsCreateOrUpdate, | ||
Read: resourceArmApplicationInsightsRead, | ||
Update: resourceArmApplicationInsightsCreateOrUpdate, | ||
Delete: resourceArmApplicationInsightsDelete, | ||
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. there's tests for import, but this resource doesn't currently support it - could we add the code to do this?
|
||
|
||
Schema: map[string]*schema.Schema{ | ||
"resource_group_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"application_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
DiffSuppressFunc: ignoreCaseDiffSuppressFunc, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
string(appinsights.Web), | ||
string(appinsights.Other), | ||
}, true), | ||
}, | ||
"location": locationSchema(), | ||
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. there's some other fields we should provide here:
|
||
"tags": tagsSchema(), | ||
"instrumentation_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmApplicationInsightsCreateOrUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient) | ||
AppInsightsClient := client.appInsightsClient | ||
|
||
log.Printf("[INFO] preparing arguments for AzureRM Application Insights creation.") | ||
|
||
name := d.Get("name").(string) | ||
resGroup := d.Get("resource_group_name").(string) | ||
applicationType := d.Get("application_type").(string) | ||
location := d.Get("location").(string) | ||
tags := d.Get("tags").(map[string]interface{}) | ||
|
||
applicationInsightsComponentProperties := appinsights.ApplicationInsightsComponentProperties{ | ||
ApplicationID: &name, | ||
ApplicationType: appinsights.ApplicationType(applicationType), | ||
} | ||
|
||
insightProperties := appinsights.ApplicationInsightsComponent{ | ||
Name: &name, | ||
Location: &location, | ||
Tags: expandTags(tags), | ||
Kind: &applicationType, | ||
ApplicationInsightsComponentProperties: &applicationInsightsComponentProperties, | ||
} | ||
|
||
_, err := AppInsightsClient.CreateOrUpdate(resGroup, name, insightProperties) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
read, err := AppInsightsClient.Get(resGroup, name) | ||
if err != nil { | ||
return err | ||
} | ||
if read.ID == nil { | ||
return fmt.Errorf("Cannot read application insights %s (resource group %s) ID", name, resGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmApplicationInsightsRead(d, meta) | ||
} | ||
|
||
func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{}) error { | ||
AppInsightsClient := meta.(*ArmClient).appInsightsClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Reading application insights %s", id) | ||
|
||
resGroup := id.ResourceGroup | ||
name := id.Path["components"] | ||
|
||
resp, err := AppInsightsClient.Get(resGroup, name) | ||
if err != nil { | ||
if resp.StatusCode == http.StatusNotFound { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error making Read request on AzureRM application insights %s: %s", name, err) | ||
} | ||
|
||
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 also set the
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. (done) |
||
if resp.ApplicationInsightsComponentProperties != nil { | ||
d.Set("application_id", resp.ApplicationInsightsComponentProperties.ApplicationID) | ||
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. From what I can see, |
||
d.Set("application_type", string(resp.ApplicationInsightsComponentProperties.ApplicationType)) | ||
d.Set("instrumentation_key", resp.ApplicationInsightsComponentProperties.InstrumentationKey) | ||
} | ||
|
||
flattenAndSetTags(d, resp.Tags) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmApplicationInsightsDelete(d *schema.ResourceData, meta interface{}) error { | ||
AppInsightsClient := meta.(*ArmClient).appInsightsClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
name := id.Path["components"] | ||
|
||
log.Printf("[DEBUG] Deleting application insights %s: %s", resGroup, name) | ||
|
||
resp, err := AppInsightsClient.Delete(resGroup, name) | ||
if err != nil { | ||
if resp.StatusCode == http.StatusNotFound { | ||
return nil | ||
} | ||
return fmt.Errorf("Error issuing AzureRM delete request for Application Insights '%s': %+v", name, err) | ||
} | ||
|
||
return err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAzureRMAppInsights_basic(t *testing.T) { | ||
|
||
ri := acctest.RandInt() | ||
config := fmt.Sprintf(testAccAzureRMApplicationInsights_basic, ri, ri) | ||
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. for newer resources we're tending to make these functions return a formatted strings, i.e. 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. (done) |
||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMAppInsightsDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMAppInsightsExists("azurerm_application_insights.test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAzureRMAppInsightsDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*ArmClient).appInsightsClient | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "azurerm_application_insights" { | ||
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 Insights still exists:\n%#v", resp.ApplicationInsightsComponentProperties) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testCheckAzureRMAppInsightsExists(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) | ||
} | ||
|
||
name := 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 Insights: %s", name) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*ArmClient).appInsightsClient | ||
|
||
resp, err := conn.Get(resourceGroup, name) | ||
if err != nil { | ||
return fmt.Errorf("Bad: Get on appInsightsClient: %s", err) | ||
} | ||
|
||
if resp.StatusCode == http.StatusNotFound { | ||
return fmt.Errorf("Bad: App Insights %q (resource group: %q) does not exist", name, resourceGroup) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
var testAccAzureRMApplicationInsights_basic = ` | ||
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. it'd be good to include a test for the 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. (done) |
||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
location = "West Europe" | ||
} | ||
resource "azurerm_application_insights" "test" { | ||
name = "acctestappinsights-%d" | ||
location = "West Europe" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
application_type = "web" | ||
} | ||
` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
could we rename these to
ApplicationInsights
for consistency?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.
(done)