Skip to content
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

New Resource: azurerm_api_management_logger #2994

Merged
merged 36 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
99a92c0
Add API Management Logger Client.
Mar 4, 2019
258f4d9
Implement API Management Logger.
Mar 4, 2019
f49572b
Add acceptance tests to API Management Logger
Mar 4, 2019
352ed77
Add documentation of API Management Logger
Mar 4, 2019
27ed518
Resolve conflicts with master branch.
Mar 4, 2019
1b7c4b9
Fix IsBuffered not updatable by using Update method
Mar 5, 2019
7a61ac7
Update azurerm/resource_arm_api_management_logger.go
tombuildsstuff Mar 19, 2019
8ccac1d
Update website/docs/r/api_management_logger.html.markdown
tombuildsstuff Mar 19, 2019
8bf46e8
Update website/docs/r/api_management_logger.html.markdown
tombuildsstuff Mar 19, 2019
26b0e8e
Update website/docs/r/api_management_logger.html.markdown
tombuildsstuff Mar 19, 2019
1608760
Update website/docs/r/api_management_logger.html.markdown
tombuildsstuff Mar 19, 2019
a77ccee
Update website/docs/r/api_management_logger.html.markdown
tombuildsstuff Mar 19, 2019
614c93b
Update azurerm/resource_arm_api_management_logger.go
tombuildsstuff Mar 19, 2019
f459858
Update azurerm/resource_arm_api_management_logger.go
tombuildsstuff Mar 19, 2019
7aaa7a9
Update azurerm/resource_arm_api_management_logger.go
tombuildsstuff Mar 19, 2019
d4436b7
Update azurerm/resource_arm_api_management_logger_test.go
tombuildsstuff Mar 20, 2019
1b6b10d
Update azurerm/resource_arm_api_management_logger_test.go
tombuildsstuff Mar 20, 2019
290d830
Update azurerm/resource_arm_api_management_logger_test.go
tombuildsstuff Mar 20, 2019
f454e9f
Update azurerm/resource_arm_api_management_logger_test.go
tombuildsstuff Mar 20, 2019
aa9d347
Update azurerm/resource_arm_api_management_logger_test.go
tombuildsstuff Mar 20, 2019
20f8c53
Update azurerm/resource_arm_api_management_logger_test.go
tombuildsstuff Mar 20, 2019
2099d06
Update azurerm/resource_arm_api_management_logger_test.go
tombuildsstuff Mar 20, 2019
2fc7ab2
Update azurerm/resource_arm_api_management_logger_test.go
tombuildsstuff Mar 20, 2019
a32a587
Update azurerm/resource_arm_api_management_logger_test.go
tombuildsstuff Mar 20, 2019
6027033
Resolve comments from code review.
Mar 20, 2019
b84c35b
Merge branch 'resource_apimng_logger' of https://github.com/terraform…
Mar 20, 2019
35e07bd
Remove application insights property checks
Mar 20, 2019
78c94e6
Fix acceptance import errors.
Mar 20, 2019
0a45935
Update azurerm/resource_arm_api_management_logger.go
tombuildsstuff Mar 20, 2019
b56e4cc
Update azurerm/resource_arm_api_management_logger_test.go
tombuildsstuff Mar 20, 2019
ddbacf8
Update website/docs/r/api_management_logger.html.markdown
tombuildsstuff Mar 20, 2019
e1b51f6
Update website/docs/r/api_management_logger.html.markdown
tombuildsstuff Mar 20, 2019
38c3f86
Update website/docs/r/api_management_logger.html.markdown
tombuildsstuff Mar 20, 2019
24b2528
Update website/docs/r/api_management_logger.html.markdown
tombuildsstuff Mar 20, 2019
88b1bea
Resolve minor comments in PR [round 2]
Mar 20, 2019
d09a04e
Ignore AI properties in Import tests
Mar 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ type ArmClient struct {
// API Management
apiManagementGroupClient apimanagement.GroupClient
apiManagementGroupUsersClient apimanagement.GroupUserClient
apiManagementLoggerClient apimanagement.LoggerClient
apiManagementProductsClient apimanagement.ProductClient
apiManagementProductGroupsClient apimanagement.ProductGroupClient
apiManagementServiceClient apimanagement.ServiceClient
Expand Down Expand Up @@ -501,6 +502,10 @@ func (c *ArmClient) registerApiManagementServiceClients(endpoint, subscriptionId
c.configureClient(&groupUsersClient.Client, auth)
c.apiManagementGroupUsersClient = groupUsersClient

loggerClient := apimanagement.NewLoggerClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&loggerClient.Client, auth)
c.apiManagementLoggerClient = loggerClient

serviceClient := apimanagement.NewServiceClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&serviceClient.Client, auth)
c.apiManagementServiceClient = serviceClient
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_api_management": resourceArmApiManagementService(),
"azurerm_api_management_group": resourceArmApiManagementGroup(),
"azurerm_api_management_group_user": resourceArmApiManagementGroupUser(),
"azurerm_api_management_logger": resourceArmApiManagementLogger(),
"azurerm_api_management_product": resourceArmApiManagementProduct(),
"azurerm_api_management_product_group": resourceArmApiManagementProductGroup(),
"azurerm_api_management_user": resourceArmApiManagementUser(),
Expand Down
256 changes: 256 additions & 0 deletions azurerm/resource_arm_api_management_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2018-01-01/apimanagement"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmApiManagementLogger() *schema.Resource {
return &schema.Resource{
Create: resourceArmApiManagementLoggerCreate,
Read: resourceArmApiManagementLoggerRead,
Update: resourceArmApiManagementLoggerUpdate,
Delete: resourceArmApiManagementLoggerDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": azure.SchemaApiManagementChildName(),

"resource_group_name": resourceGroupNameSchema(),

"api_management_name": azure.SchemaApiManagementName(),

"eventhub": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"application_insights"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateEventHubName(),
},

"connection_string": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
},
},

"application_insights": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"eventhub"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instrumentation_key": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
ValidateFunc: validate.NoEmptyStrings,
},
},
},
},

"buffered": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},

"description": {
Type: schema.TypeString,
Optional: true,
},
},
}
}

func resourceArmApiManagementLoggerCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementLoggerClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)

eventHubRaw := d.Get("eventhub").([]interface{})
appInsightsRaw := d.Get("application_insights").([]interface{})

if len(eventHubRaw) == 0 && len(appInsightsRaw) == 0 {
return fmt.Errorf("Either `eventhub` or `application_insights` is required")
}

parameters := apimanagement.LoggerContract{
LoggerContractProperties: &apimanagement.LoggerContractProperties{
IsBuffered: utils.Bool(d.Get("buffered").(bool)),
Description: utils.String(d.Get("description").(string)),
},
}

if len(eventHubRaw) > 0 {
parameters.LoggerType = apimanagement.AzureEventHub
parameters.Credentials = expandArmApiManagementLoggerEventHub(eventHubRaw)
} else if len(appInsightsRaw) > 0 {
parameters.LoggerType = apimanagement.ApplicationInsights
parameters.Credentials = expandArmApiManagementLoggerApplicationInsights(appInsightsRaw)
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, name, parameters, ""); err != nil {
return fmt.Errorf("Error creating Logger %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err)
}

resp, err := client.Get(ctx, resourceGroup, serviceName, name)
if err != nil {
return fmt.Errorf("Error retrieving Logger %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err)
}
if resp.ID == nil {
return fmt.Errorf("Cannot read Logger %q (Resource Group %q / API Management Service %q) ID", name, resourceGroup, serviceName)
}
d.SetId(*resp.ID)

return resourceArmApiManagementLoggerRead(d, meta)
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
}

func resourceArmApiManagementLoggerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementLoggerClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
name := id.Path["loggers"]

resp, err := client.Get(ctx, resourceGroup, serviceName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Logger %q (API Management Service %q / Resource Group %q) was not found - removing from state", name, serviceName, resourceGroup)
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Logger %q (API Management Service %q / Resource Group %q): %+v", name, serviceName, resourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("api_management_name", serviceName)

if properties := resp.LoggerContractProperties; properties != nil {
d.Set("buffered", properties.IsBuffered)
d.Set("description", properties.Description)
if err := d.Set("eventhub", flattenArmApiManagementLoggerEventHub(d, properties)); err != nil {
return fmt.Errorf("Error setting `eventhub` for Logger %q (Resource Group %q / API Management Name %q): %s", name, resourceGroup, serviceName, err)
JunyiYi marked this conversation as resolved.
Show resolved Hide resolved
}
JunyiYi marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

func resourceArmApiManagementLoggerUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementLoggerClient
ctx := meta.(*ArmClient).StopContext

resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
name := d.Get("name").(string)

eventHubRaw, hasEventHub := d.GetOk("eventhub")
appInsightsRaw, hasAppInsights := d.GetOk("application_insights")

parameters := apimanagement.LoggerUpdateContract{
LoggerUpdateParameters: &apimanagement.LoggerUpdateParameters{
IsBuffered: utils.Bool(d.Get("buffered").(bool)),
Description: utils.String(d.Get("description").(string)),
},
}

if hasEventHub {
parameters.LoggerType = apimanagement.AzureEventHub
parameters.Credentials = expandArmApiManagementLoggerEventHub(eventHubRaw.([]interface{}))
} else if hasAppInsights {
parameters.LoggerType = apimanagement.ApplicationInsights
parameters.Credentials = expandArmApiManagementLoggerApplicationInsights(appInsightsRaw.([]interface{}))
}

if _, err := client.Update(ctx, resourceGroup, serviceName, name, parameters, ""); err != nil {
return fmt.Errorf("Error updating Logger %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err)
}

return resourceArmApiManagementLoggerRead(d, meta)
}

func resourceArmApiManagementLoggerDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).apiManagementLoggerClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return fmt.Errorf("Error parsing API Management Logger ID %q: %+v", d.Id(), err)
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
name := id.Path["loggers"]

if resp, err := client.Delete(ctx, resourceGroup, serviceName, name, ""); err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error deleting Logger %q (Resource Group %q / API Management Service %q): %+v", name, resourceGroup, serviceName, err)
}
}

return nil
}

func expandArmApiManagementLoggerEventHub(input []interface{}) map[string]*string {
credentials := make(map[string]*string)
eventHub := input[0].(map[string]interface{})
credentials["name"] = utils.String(eventHub["name"].(string))
credentials["connectionString"] = utils.String(eventHub["connection_string"].(string))
return credentials
}

func expandArmApiManagementLoggerApplicationInsights(input []interface{}) map[string]*string {
credentials := make(map[string]*string)
ai := input[0].(map[string]interface{})
credentials["instrumentationKey"] = utils.String(ai["instrumentation_key"].(string))
return credentials
}

func flattenArmApiManagementLoggerEventHub(d *schema.ResourceData, properties *apimanagement.LoggerContractProperties) []interface{} {
if properties.LoggerType != apimanagement.AzureEventHub {
JunyiYi marked this conversation as resolved.
Show resolved Hide resolved
return []interface{}{}
}
eventHub := make(map[string]interface{})
if name := properties.Credentials["name"]; name != nil {
eventHub["name"] = *name
}
if existing := d.Get("eventhub").([]interface{}); len(existing) > 0 {
existingEventHub := existing[0].(map[string]interface{})
if conn, ok := existingEventHub["connection_string"]; ok {
eventHub["connection_string"] = conn.(string)
}
}

return []interface{}{eventHub}
}
Loading