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

add new resource & data source: azurerm_netapp_account #4416

Merged
merged 17 commits into from
Nov 15, 2019
3 changes: 3 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mssql"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/mysql"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/netapp"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/notificationhub"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/policy"
Expand Down Expand Up @@ -124,6 +125,7 @@ type ArmClient struct {
Msi *msi.Client
Mssql *mssql.Client
Mysql *mysql.Client
Netapp *netapp.Client
Network *network.Client
NotificationHubs *notificationhub.Client
Policy *policy.Client
Expand Down Expand Up @@ -258,6 +260,7 @@ func getArmClient(authConfig *authentication.Config, skipProviderRegistration bo
client.Msi = msi.BuildClient(o)
client.Mysql = mysql.BuildClient(o)
client.ManagementGroups = managementgroup.BuildClient(o)
client.Netapp = netapp.BuildClient(o)
client.Network = network.BuildClient(o)
client.NotificationHubs = notificationhub.BuildClient(o)
client.Policy = policy.BuildClient(o)
Expand Down
108 changes: 108 additions & 0 deletions azurerm/data_source_netapp_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
aznetapp "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/netapp"
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmNetAppAccount() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmNetAppAccountRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: aznetapp.ValidateNetAppAccountName,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved

"location": azure.SchemaLocationForDataSource(),

"active_directory": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dns_servers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"domain": {
Type: schema.TypeString,
Computed: true,
},
"smb_server_name": {
Type: schema.TypeString,
Computed: true,
},
"username": {
Type: schema.TypeString,
Computed: true,
},
"password": {
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Sensitive: true,
Computed: true,
},
"organizational_unit": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"tags": tags.SchemaDataSource(),
},
}
}

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

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

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: NetApp Account %q (Resource Group %q) was not found", name, resourceGroup)
}
return fmt.Errorf("Error reading NetApp Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
if props := resp.AccountProperties; props != nil {
if err := d.Set("active_directory", flattenArmNetAppActiveDirectories(props.ActiveDirectories)); err != nil {
return fmt.Errorf("Error setting `active_directory`: %+v", err)
}
}

currentTags := make(map[string]*string)
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
if v := resp.Tags; v != nil {
tagMap := v.(map[string]interface{})

for k, v := range tagMap {
currentTags[k] = utils.String(v.(string))
}
}
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved

return tags.FlattenAndSet(d, currentTags)
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
}
41 changes: 41 additions & 0 deletions azurerm/data_source_netapp_account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceAzureRMNetAppAccount_basic(t *testing.T) {
dataSourceName := "data.azurerm_netapp_account.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceNetAppAccount_basic(ri, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
),
},
},
})
}

func testAccDataSourceNetAppAccount_basic(rInt int, location string) string {
config := testAccAzureRMNetAppAccount_basic(rInt, location)
return fmt.Sprintf(`
%s

data "azurerm_netapp_account" "test" {
resource_group_name = "${azurerm_netapp_account.test.resource_group_name}"
name = "${azurerm_netapp_account.test.name}"
}
`, config)
}
19 changes: 19 additions & 0 deletions azurerm/internal/services/netapp/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package netapp

import (
"github.com/Azure/azure-sdk-for-go/services/netapp/mgmt/2019-06-01/netapp"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

type Client struct {
AccountClient *netapp.AccountsClient
}

func BuildClient(o *common.ClientOptions) *Client {
AccountClient := netapp.NewAccountsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
o.ConfigureClient(&AccountClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AccountClient: &AccountClient,
}
}
42 changes: 42 additions & 0 deletions azurerm/internal/services/netapp/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package netapp

import (
"fmt"
"net"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
)

func ValidateNetAppAccountName(i interface{}, k string) (_ []string, errors []error) {
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
if m, regexErrs := validate.RegExHelper(i, k, `(^[\da-zA-Z])([-\da-zA-Z]{1,62})([\da-zA-Z]$)`); !m {
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
errors = append(regexErrs, fmt.Errorf(`%q must be between 3 and 64 characters in length and begin with a letter or number, end with a letter or number and may contain only letters, numbers or hyphens.`, k))
}

return nil, errors
}

func ValidateActiveDirectoryDomainName(i interface{}, k string) (_ []string, errors []error) {
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
if m, regexErrs := validate.RegExHelper(i, k, `(^(.*)[\da-zA-Z]\.[\da-zA-Z].{1,}$)`); !m {
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
errors = append(regexErrs, fmt.Errorf(`%q must end with a letter or number before dot and start with a letter or number after dot.`, k))
}

if len(k) > 255 {
errors = append(errors, fmt.Errorf(`Active Directory Domain Name can not be longer than 255 characters in length, got %d characters`, len(k)))
}

return nil, errors
}

func ValidateActiveDirectoryDNSName(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
return nil, []error{fmt.Errorf("expected type of %q to be string", k)}
}

result := net.ParseIP(v)
if result.To4() == nil {
errors = append(errors, fmt.Errorf("%q is not a valid IPv4 IP address", v))
}

return nil, errors
}
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_monitor_diagnostic_categories": dataSourceArmMonitorDiagnosticCategories(),
"azurerm_monitor_log_profile": dataSourceArmMonitorLogProfile(),
"azurerm_mssql_elasticpool": dataSourceArmMsSqlElasticpool(),
"azurerm_netapp_account": dataSourceArmNetAppAccount(),
"azurerm_network_ddos_protection_plan": dataSourceNetworkDDoSProtectionPlan(),
"azurerm_network_interface": dataSourceArmNetworkInterface(),
"azurerm_network_security_group": dataSourceArmNetworkSecurityGroup(),
Expand Down Expand Up @@ -353,6 +354,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_network_security_group": resourceArmNetworkSecurityGroup(),
"azurerm_network_security_rule": resourceArmNetworkSecurityRule(),
"azurerm_network_watcher": resourceArmNetworkWatcher(),
"azurerm_netapp_account": resourceArmNetAppAccount(),
"azurerm_notification_hub_authorization_rule": resourceArmNotificationHubAuthorizationRule(),
"azurerm_notification_hub_namespace": resourceArmNotificationHubNamespace(),
"azurerm_notification_hub": resourceArmNotificationHub(),
Expand Down
Loading