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
93 changes: 93 additions & 0 deletions azurerm/data_source_netapp_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
netAppSvc "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/netapp"
"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: netAppSvc.ValidateNetAppAccountName,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"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,
},
"organizational_unit": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

// Handles tags being interface{} until https://github.com/Azure/azure-rest-api-specs/issues/7447 is fixed
},
}
}

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, d)); err != nil {
return fmt.Errorf("Error setting `active_directory`: %+v", err)
}
}

return nil
}
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_basicConfig(ri, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
),
},
},
})
}

func testAccDataSourceNetAppAccount_basicConfig(rInt int, location string) string {
config := testAccAzureRMNetAppAccount_basicConfig(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)
o.ConfigureClient(&accountClient.Client, o.ResourceManagerAuthorizer)

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

import (
"fmt"

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

func ValidateNetAppAccountName(i interface{}, k string) (_ []string, errors []error) {
if m, regexErrs := validate.RegExHelper(i, k, `(^[\da-zA-Z])([-\da-zA-Z]{1,62})([\da-zA-Z]$)`); !m {
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) {
if m, regexErrs := validate.RegExHelper(i, k, `(^(.*)[\da-zA-Z]\.[\da-zA-Z].{1,}$)`); !m {
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 ValidateActiveDirectorySMBServerName(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
return nil, append(errors, fmt.Errorf("expected type of %q to be string", k))
}

if len(v) > 10 {
errors = append(errors, fmt.Errorf(`SMB server name can not be longer than 10 characters in length, got %d characters`, len(v)))
}

return nil, errors
}
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