-
Notifications
You must be signed in to change notification settings - Fork 850
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from jen20/virtual-network-client
Add a client for Virtual Network management
- Loading branch information
Showing
3 changed files
with
166 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package vnetClient | ||
|
||
import ( | ||
"encoding/xml" | ||
) | ||
|
||
const xmlNamespace = "http://schemas.microsoft.com/ServiceHosting/2011/07/NetworkConfiguration" | ||
const xmlNamespaceXsd = "http://www.w3.org/2001/XMLSchema" | ||
const xmlNamespaceXsi = "http://www.w3.org/2001/XMLSchema-instance" | ||
|
||
//NetworkConfiguration represents the network configuration for an entire Azure | ||
//subscription. TODO: Nicer builder methods for these that abstract away the | ||
//underlying structure | ||
type NetworkConfiguration struct { | ||
XMLName xml.Name `xml:"NetworkConfiguration"` | ||
XmlNamespaceXsd string `xml:"xmlns:xsd,attr"` | ||
XmlNamespaceXsi string `xml:"xmlns:xsi,attr"` | ||
Xmlns string `xml:"xmlns,attr"` | ||
Configuration VirtualNetworkConfiguration `xml:"VirtualNetworkConfiguration"` | ||
} | ||
|
||
//NewNetworkConfiguration creates a new empty NetworkConfiguration structure for | ||
//further configuration. The XML namespaces are set correctly. | ||
func NewNetworkConfiguration() NetworkConfiguration { | ||
networkConfiguration := NetworkConfiguration{} | ||
networkConfiguration.setXmlNamespaces() | ||
return networkConfiguration | ||
} | ||
|
||
//setXmlNamespaces ensure that all of the required namespaces are set. It should | ||
//be called prior to marshalling the structure to XML for use with the Azure REST | ||
//endpoint. It is used internally prior to submitting requests, but since it is | ||
//idempotent there is no harm in repeat calls. | ||
func (self *NetworkConfiguration) setXmlNamespaces() { | ||
self.XmlNamespaceXsd = xmlNamespaceXsd | ||
self.XmlNamespaceXsi = xmlNamespaceXsi | ||
self.Xmlns = xmlNamespace | ||
} | ||
|
||
type VirtualNetworkConfiguration struct { | ||
Dns Dns `xml:"Dns,omitempty"` | ||
LocalNetworkSites []LocalNetworkSite `xml:"LocalNetworkSites>LocalNetworkSite"` | ||
VirtualNetworkSites []VirtualNetworkSite `xml:"VirtualNetworkSites>VirtualNetworkSite"` | ||
} | ||
|
||
type Dns struct { | ||
DnsServers []DnsServer `xml:"DnsServers,omitempty>DnsServer,omitempty"` | ||
} | ||
|
||
type DnsServer struct { | ||
XMLName xml.Name `xml:"DnsServer"` | ||
Name string `xml:"name,attr"` | ||
IPAddress string `xml:"IPAddress,attr"` | ||
} | ||
|
||
type DnsServerRef struct { | ||
Name string `xml:"name,attr"` | ||
} | ||
|
||
type VirtualNetworkSite struct { | ||
Name string `xml:"name,attr"` | ||
Location string `xml:"Location,attr"` | ||
AddressSpace AddressSpace `xml:"AddressSpace"` | ||
Subnets []Subnet `xml:"Subnets>Subnet"` | ||
DnsServersRef []DnsServerRef `xml:"DnsServersRef,omitempty>DnsServerRef"` | ||
} | ||
|
||
type LocalNetworkSite struct { | ||
Name string `xml:"name,attr"` | ||
VPNGatewayAddress string | ||
AddressSpace AddressSpace | ||
} | ||
|
||
type AddressSpace struct { | ||
AddressPrefix []string | ||
} | ||
|
||
type Subnet struct { | ||
Name string `xml:"name,attr"` | ||
AddressPrefix string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package vnetClient | ||
|
||
import ( | ||
"encoding/xml" | ||
azure "github.com/MSOpenTech/azure-sdk-for-go" | ||
) | ||
|
||
const ( | ||
azureNetworkConfigurationURL = "services/networking/media" | ||
) | ||
|
||
//GetVirtualNetworkConfiguration retreives the current virtual network | ||
//configuration for the currently active subscription. Note that the | ||
//underlying Azure API means that network related operations are not safe | ||
//for running concurrently. | ||
func GetVirtualNetworkConfiguration() (NetworkConfiguration, error) { | ||
networkConfiguration := NewNetworkConfiguration() | ||
response, err := azure.SendAzureGetRequest(azureNetworkConfigurationURL) | ||
if err != nil { | ||
return networkConfiguration, err | ||
} | ||
|
||
err = xml.Unmarshal(response, &networkConfiguration) | ||
if err != nil { | ||
return networkConfiguration, err | ||
} | ||
|
||
return networkConfiguration, nil | ||
} | ||
|
||
//SetVirtualNetworkConfiguration configures the virtual networks for the | ||
//currently active subscription according to the NetworkConfiguration given. | ||
//Note that the underlying Azure API means that network related operations | ||
//are not safe for running concurrently. | ||
func SetVirtualNetworkConfiguration(networkConfiguration NetworkConfiguration) error { | ||
networkConfiguration.setXmlNamespaces() | ||
networkConfigurationBytes, err := xml.Marshal(networkConfiguration) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
requestId, err := azure.SendAzurePutRequest(azureNetworkConfigurationURL, "text/plain", networkConfigurationBytes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = azure.WaitAsyncOperation(requestId) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters