Skip to content

Commit

Permalink
Merge pull request #571 from terraform-providers/network-watcher
Browse files Browse the repository at this point in the history
New Resource: `azurerm_network_watcher`
  • Loading branch information
tombuildsstuff authored Nov 21, 2017
2 parents 7af9693 + c8141c7 commit b247913
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 0 deletions.
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

0 comments on commit b247913

Please sign in to comment.