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_logz_sub_account_tag_rule #17557

Merged
merged 4 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 11 additions & 6 deletions internal/services/logz/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
)

type Client struct {
MonitorClient *logz.MonitorsClient
TagRuleClient *logz.TagRulesClient
SubAccountClient *logz.SubAccountClient
MonitorClient *logz.MonitorsClient
TagRuleClient *logz.TagRulesClient
SubAccountClient *logz.SubAccountClient
SubAccountTagRuleClient *logz.SubAccountTagRulesClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -21,9 +22,13 @@ func NewClient(o *common.ClientOptions) *Client {
subAccountClient := logz.NewSubAccountClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&subAccountClient.Client, o.ResourceManagerAuthorizer)

subAccountTagRuleClient := logz.NewSubAccountTagRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&subAccountTagRuleClient.Client, o.ResourceManagerAuthorizer)

return &Client{
MonitorClient: &monitorClient,
TagRuleClient: &tagRuleClient,
SubAccountClient: &subAccountClient,
MonitorClient: &monitorClient,
TagRuleClient: &tagRuleClient,
SubAccountClient: &subAccountClient,
SubAccountTagRuleClient: &subAccountTagRuleClient,
}
}
33 changes: 33 additions & 0 deletions internal/services/logz/logz_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

const TagRuleName = "default"

func SchemaUserInfo() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Expand Down Expand Up @@ -95,3 +97,34 @@ func flattenUserInfo(input *logz.UserInfo) []interface{} {
},
}
}

func schemaTagFilter() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 10,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"action": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(logz.TagActionInclude),
string(logz.TagActionExclude),
}, false),
},

"value": {
Type: pluginsdk.TypeString,
Optional: true,
},
},
},
}
}
154 changes: 154 additions & 0 deletions internal/services/logz/logz_sub_account_tag_rule_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package logz

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/logz/mgmt/2020-10-01/logz"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/logz/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/logz/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceLogzSubAccountTagRule() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceLogzSubAccountTagRuleCreateUpdate,
Read: resourceLogzSubAccountTagRuleRead,
Update: resourceLogzSubAccountTagRuleCreateUpdate,
Delete: resourceLogzSubAccountTagRuleDelete,

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.LogzSubAccountTagRuleID(id)
return err
}),

Schema: map[string]*pluginsdk.Schema{
"logz_sub_account_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.LogzSubAccountID,
},

"tag_filter": schemaTagFilter(),

"send_aad_logs": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"send_activity_logs": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"send_subscription_logs": {
ms-zhenhua marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceLogzSubAccountTagRuleCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Logz.SubAccountTagRuleClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

subAccountId, err := parse.LogzSubAccountID(d.Get("logz_sub_account_id").(string))
if err != nil {
return err
}

id := parse.NewLogzSubAccountTagRuleID(subAccountId.SubscriptionId, subAccountId.ResourceGroup, subAccountId.MonitorName, subAccountId.AccountName, TagRuleName)

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.MonitorName, id.AccountName, id.TagRuleName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_logz_sub_account_tag_rule", *existing.ID)
}
}

props := logz.MonitoringTagRules{
Properties: &logz.MonitoringTagRulesProperties{
LogRules: expandTagRuleLogRules(d),
},
}

if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.MonitorName, id.AccountName, id.TagRuleName, &props); err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

d.SetId(id.ID())
return resourceLogzSubAccountTagRuleRead(d, meta)
}

func resourceLogzSubAccountTagRuleRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Logz.SubAccountTagRuleClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.LogzSubAccountTagRuleID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.MonitorName, id.AccountName, id.TagRuleName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] logz %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.Set("logz_sub_account_id", parse.NewLogzSubAccountID(id.SubscriptionId, id.ResourceGroup, id.MonitorName, id.AccountName).ID())
if props := resp.Properties; props != nil && props.LogRules != nil {
d.Set("send_aad_logs", props.LogRules.SendAadLogs)
d.Set("send_activity_logs", props.LogRules.SendActivityLogs)
d.Set("send_subscription_logs", props.LogRules.SendSubscriptionLogs)

if err := d.Set("tag_filter", flattenTagRuleFilteringTagArray(props.LogRules.FilteringTags)); err != nil {
return fmt.Errorf("setting `tag_filter`: %+v", err)
}
}

return nil
}

func resourceLogzSubAccountTagRuleDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Logz.SubAccountTagRuleClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.LogzSubAccountTagRuleID(d.Id())
if err != nil {
return err
}

if _, err := client.Delete(ctx, id.ResourceGroup, id.MonitorName, id.AccountName, id.TagRuleName); err != nil {
return fmt.Errorf("deleting %s: %+v", id, err)
}

return nil
}
Loading