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_network_watcher #571

Merged
merged 1 commit into from
Nov 21, 2017
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
13 changes: 13 additions & 0 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ type ArmClient struct {
sqlElasticPoolsClient sql.ElasticPoolsClient
sqlFirewallRulesClient sql.FirewallRulesClient
sqlServersClient sql.ServersClient

// Networking
watcherClient network.WatchersClient
}

func withRequestLogging() autorest.SendDecorator {
Expand Down Expand Up @@ -655,11 +658,21 @@ func (c *Config) getArmClient() (*ArmClient, error) {
client.registerDatabases(endpoint, c.SubscriptionID, auth, sender)
client.registerDisks(endpoint, c.SubscriptionID, auth, sender)
client.registerKeyVaultClients(endpoint, c.SubscriptionID, auth, keyVaultAuth, sender)
client.registerNetworkingClients(endpoint, c.SubscriptionID, auth, sender)
client.registerRedisClients(endpoint, c.SubscriptionID, auth, sender)

return &client, nil
}

func (c *ArmClient) registerNetworkingClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
// TODO: move the other networking stuff in here, gradually
watchersClient := network.NewWatchersClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&watchersClient.Client)
watchersClient.Authorizer = auth
watchersClient.Sender = sender
c.watcherClient = watchersClient
}

func (c *ArmClient) registerAuthentication(endpoint, graphEndpoint, subscriptionId, tenantId string, auth, graphAuth autorest.Authorizer, sender autorest.Sender) {
spc := graphrbac.NewServicePrincipalsClientWithBaseURI(graphEndpoint, tenantId)
setUserAgent(&spc.Client)
Expand Down
50 changes: 50 additions & 0 deletions azurerm/import_arm_network_watcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package azurerm

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMNetworkWatcher_importBasic(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "azurerm_network_watcher.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMNetworkWatcherDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMNetworkWatcher_basic(rInt, testLocation()),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMNetworkWatcher_importComplete(t *testing.T) {
rInt := acctest.RandInt()
resourceName := "azurerm_network_watcher.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMNetworkWatcherDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMNetworkWatcher_complete(rInt, testLocation()),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_network_interface": resourceArmNetworkInterface(),
"azurerm_network_security_group": resourceArmNetworkSecurityGroup(),
"azurerm_network_security_rule": resourceArmNetworkSecurityRule(),
"azurerm_network_watcher": resourceArmNetworkWatcher(),
"azurerm_postgresql_configuration": resourceArmPostgreSQLConfiguration(),
"azurerm_postgresql_database": resourceArmPostgreSQLDatabase(),
"azurerm_postgresql_firewall_rule": resourceArmPostgreSQLFirewallRule(),
Expand Down
116 changes: 116 additions & 0 deletions azurerm/resource_arm_network_watcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package azurerm

import (
"fmt"

"github.com/Azure/azure-sdk-for-go/arm/network"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmNetworkWatcher() *schema.Resource {
return &schema.Resource{
Create: resourceArmNetworkWatcherCreateUpdate,
Read: resourceArmNetworkWatcherRead,
Update: resourceArmNetworkWatcherCreateUpdate,
Delete: resourceArmNetworkWatcherDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

"resource_group_name": resourceGroupNameSchema(),

"location": locationSchema(),

"tags": tagsSchema(),
},
}
}

func resourceArmNetworkWatcherCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).watcherClient

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
location := d.Get("location").(string)
tags := d.Get("tags").(map[string]interface{})

watcher := network.Watcher{
Location: utils.String(location),
Tags: expandTags(tags),
}
_, err := client.CreateOrUpdate(resourceGroup, name, watcher)
if err != nil {
return err
}

read, err := client.Get(resourceGroup, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Network Watcher %q (Resource Group %q) ID", name, resourceGroup)
}

d.SetId(*read.ID)

return resourceArmNetworkWatcherRead(d, meta)
}

func resourceArmNetworkWatcherRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).watcherClient

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

resp, err := client.Get(resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Network Watcher %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("location", azureRMNormalizeLocation(*resp.Location))

flattenAndSetTags(d, resp.Tags)

return nil
}

func resourceArmNetworkWatcherDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).watcherClient

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

deleteResp, deleteErr := client.Delete(resourceGroup, name, make(chan struct{}))
resp := <-deleteResp
err = <-deleteErr

if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error deleting Network Watcher %q (Resource Group %q): %+v", name, resourceGroup, err)
}
}

return nil
}
Loading