Skip to content

Commit

Permalink
New Resource: azurerm_logz_sub_account (#16581)
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-zhenhua authored Jul 6, 2022
1 parent 1679275 commit c8a8897
Show file tree
Hide file tree
Showing 15 changed files with 1,045 additions and 96 deletions.
13 changes: 9 additions & 4 deletions internal/services/logz/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

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

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -17,8 +18,12 @@ func NewClient(o *common.ClientOptions) *Client {
tagRuleClient := logz.NewTagRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&tagRuleClient.Client, o.ResourceManagerAuthorizer)

subAccountClient := logz.NewSubAccountClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&subAccountClient.Client, o.ResourceManagerAuthorizer)

return &Client{
MonitorClient: &monitorClient,
TagRuleClient: &tagRuleClient,
MonitorClient: &monitorClient,
TagRuleClient: &tagRuleClient,
SubAccountClient: &subAccountClient,
}
}
97 changes: 97 additions & 0 deletions internal/services/logz/logz_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package logz

import (
"github.com/Azure/azure-sdk-for-go/services/logz/mgmt/2020-10-01/logz"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func SchemaUserInfo() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"email": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"first_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 50),
},

"last_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 50),
},

"phone_number": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 40),
},
},
},
}
}

func expandUserInfo(input []interface{}) *logz.UserInfo {
if len(input) == 0 || input[0] == nil {
return nil
}

v := input[0].(map[string]interface{})
return &logz.UserInfo{
FirstName: utils.String(v["first_name"].(string)),
LastName: utils.String(v["last_name"].(string)),
EmailAddress: utils.String(v["email"].(string)),
PhoneNumber: utils.String(v["phone_number"].(string)),
}
}

func flattenUserInfo(input *logz.UserInfo) []interface{} {
if input == nil {
return make([]interface{}, 0)
}

var firstName string
if input.FirstName != nil {
firstName = *input.FirstName
}

var lastName string
if input.LastName != nil {
lastName = *input.LastName
}

var email string
if input.EmailAddress != nil {
email = *input.EmailAddress
}

var phoneNumber string
if input.PhoneNumber != nil {
phoneNumber = *input.PhoneNumber
}

return []interface{}{
map[string]interface{}{
"first_name": firstName,
"last_name": lastName,
"email": email,
"phone_number": phoneNumber,
},
}
}
72 changes: 11 additions & 61 deletions internal/services/logz/logz_monitor_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package logz
import (
"fmt"
"log"
"regexp"
"time"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/logz/validate"

"github.com/Azure/azure-sdk-for-go/services/logz/mgmt/2020-10-01/logz"
"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
Expand Down Expand Up @@ -42,13 +43,10 @@ func resourceLogzMonitor() *pluginsdk.Resource {

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[\w\-]{1,32}$`),
`The name length must be from 1 to 32 characters. The name can only contain letters, numbers, hyphens and underscore.`,
),
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.LogzMonitorName,
},

"resource_group_name": azure.SchemaResourceGroupName(),
Expand Down Expand Up @@ -126,43 +124,7 @@ func resourceLogzMonitor() *pluginsdk.Resource {
},
},

"user": {
Type: pluginsdk.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"email": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"first_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 50),
},

"last_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 50),
},

"phone_number": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 40),
},
},
},
},
"user": SchemaUserInfo(),

"enabled": {
Type: pluginsdk.TypeBool,
Expand Down Expand Up @@ -204,7 +166,7 @@ func resourceLogzMonitorCreate(d *pluginsdk.ResourceData, meta interface{}) erro
Properties: &logz.MonitorProperties{
LogzOrganizationProperties: expandMonitorOrganizationProperties(d),
PlanData: expandMonitorPlanData(d.Get("plan").([]interface{})),
UserInfo: expandMonitorUserInfo(d.Get("user").([]interface{})),
UserInfo: expandUserInfo(d.Get("user").([]interface{})),
MonitoringStatus: monitoringStatus,
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
Expand Down Expand Up @@ -255,7 +217,9 @@ func resourceLogzMonitorRead(d *pluginsdk.ResourceData, meta interface{}) error
return fmt.Errorf("setting `plan`: %+v", err)
}

d.Set("user", d.Get("user"))
if err := d.Set("user", flattenUserInfo(expandUserInfo(d.Get("user").([]interface{})))); err != nil {
return fmt.Errorf("setting `user`: %+v", err)
}
}

return tags.FlattenAndSet(d, resp.Tags)
Expand Down Expand Up @@ -346,20 +310,6 @@ func expandMonitorPlanData(input []interface{}) *logz.PlanData {
}
}

func expandMonitorUserInfo(input []interface{}) *logz.UserInfo {
if len(input) == 0 || input[0] == nil {
return nil
}

v := input[0].(map[string]interface{})
return &logz.UserInfo{
FirstName: utils.String(v["first_name"].(string)),
LastName: utils.String(v["last_name"].(string)),
EmailAddress: utils.String(v["email"].(string)),
PhoneNumber: utils.String(v["phone_number"].(string)),
}
}

func flattenMonitorOrganizationProperties(d *pluginsdk.ResourceData, input *logz.OrganizationProperties) {
if input == nil {
return
Expand Down
36 changes: 15 additions & 21 deletions internal/services/logz/logz_monitor_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package logz_test
import (
"context"
"fmt"
"strconv"
"testing"
"time"

"github.com/google/uuid"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand All @@ -22,7 +20,7 @@ func TestAccLogzMonitor_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_logz_monitor", "test")
r := LogzMonitorResource{}
effectiveDate := time.Now().Add(time.Hour * 7).Format(time.RFC3339)
email := uuid.New().String()
email := "[email protected]"
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data, effectiveDate, email),
Expand All @@ -38,7 +36,7 @@ func TestAccLogzMonitor_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_logz_monitor", "test")
r := LogzMonitorResource{}
effectiveDate := time.Now().Add(time.Hour * 7).Format(time.RFC3339)
email := uuid.New().String()
email := "[email protected]"
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data, effectiveDate, email),
Expand All @@ -57,7 +55,7 @@ func TestAccLogzMonitor_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_logz_monitor", "test")
r := LogzMonitorResource{}
effectiveDate := time.Now().Add(time.Hour * 7).Format(time.RFC3339)
email := uuid.New().String()
email := "[email protected]"
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data, effectiveDate, email),
Expand All @@ -73,7 +71,7 @@ func TestAccLogzMonitor_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_logz_monitor", "test")
r := LogzMonitorResource{}
effectiveDate := time.Now().Add(time.Hour * 7).Format(time.RFC3339)
email := uuid.New().String()
email := "[email protected]"
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data, effectiveDate, email),
Expand Down Expand Up @@ -133,7 +131,7 @@ func (r LogzMonitorResource) basic(data acceptance.TestData, effectiveDate strin
%s
resource "azurerm_logz_monitor" "test" {
name = "%s"
name = "acctest-lm-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
plan {
Expand All @@ -144,13 +142,13 @@ resource "azurerm_logz_monitor" "test" {
}
user {
email = "%s@example.com"
email = "%s"
first_name = "first"
last_name = "last"
phone_number = "123456"
}
}
`, template, getLogzInstanceName(data.RandomInteger), effectiveDate, email)
`, template, data.RandomInteger, effectiveDate, email)
}

func (r LogzMonitorResource) update(data acceptance.TestData, effectiveDate string, email string) string {
Expand All @@ -159,7 +157,7 @@ func (r LogzMonitorResource) update(data acceptance.TestData, effectiveDate stri
%s
resource "azurerm_logz_monitor" "test" {
name = "%s"
name = "acctest-lm-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
plan {
Expand All @@ -170,14 +168,14 @@ resource "azurerm_logz_monitor" "test" {
}
user {
email = "%s@example.com"
email = "%s"
first_name = "first"
last_name = "last"
phone_number = "123456"
}
enabled = false
}
`, template, getLogzInstanceName(data.RandomInteger), effectiveDate, email)
`, template, data.RandomInteger, effectiveDate, email)
}

func (r LogzMonitorResource) requiresImport(data acceptance.TestData, effectiveDate string, email string) string {
Expand All @@ -197,7 +195,7 @@ resource "azurerm_logz_monitor" "import" {
}
user {
email = "%s@example.com"
email = "%s"
first_name = "first"
last_name = "last"
phone_number = "123456"
Expand All @@ -212,7 +210,7 @@ func (r LogzMonitorResource) complete(data acceptance.TestData, effectiveDate st
%s
resource "azurerm_logz_monitor" "test" {
name = "%s"
name = "acctest-lm-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
Expand All @@ -226,19 +224,15 @@ resource "azurerm_logz_monitor" "test" {
}
user {
email = "%s@example.com"
email = "%s"
first_name = "first"
last_name = "last"
phone_number = "123456"
}
enabled = false
enabled = true
tags = {
ENV = "Test"
}
}
`, template, getLogzInstanceName(data.RandomInteger), effectiveDate, email)
}

func getLogzInstanceName(randomInteger int) string {
return "liftr_test_only_" + strconv.Itoa(randomInteger)[2:]
`, template, data.RandomInteger, effectiveDate, email)
}
Loading

0 comments on commit c8a8897

Please sign in to comment.