...

Source file src/github.com/Azure/azure-sdk-for-go/services/classic/management/networksecuritygroup/client.go

Documentation: github.com/Azure/azure-sdk-for-go/services/classic/management/networksecuritygroup

     1  // +build go1.7
     2  
     3  // Package networksecuritygroup provides a client for Network Security Groups.
     4  package networksecuritygroup
     5  
     6  // Copyright (c) Microsoft Corporation. All rights reserved.
     7  // Licensed under the MIT License. See License.txt in the project root for license information.
     8  
     9  import (
    10  	"encoding/xml"
    11  	"fmt"
    12  
    13  	"github.com/Azure/azure-sdk-for-go/services/classic/management"
    14  )
    15  
    16  const (
    17  	createSecurityGroupURL           = "services/networking/networksecuritygroups"
    18  	deleteSecurityGroupURL           = "services/networking/networksecuritygroups/%s"
    19  	getSecurityGroupURL              = "services/networking/networksecuritygroups/%s?detaillevel=full"
    20  	listSecurityGroupsURL            = "services/networking/networksecuritygroups"
    21  	addSecurityGroupToSubnetURL      = "services/networking/virtualnetwork/%s/subnets/%s/networksecuritygroups"
    22  	getSecurityGroupForSubnetURL     = "services/networking/virtualnetwork/%s/subnets/%s/networksecuritygroups"
    23  	removeSecurityGroupFromSubnetURL = "services/networking/virtualnetwork/%s/subnets/%s/networksecuritygroups/%s"
    24  	setSecurityGroupRuleURL          = "services/networking/networksecuritygroups/%s/rules/%s"
    25  	deleteSecurityGroupRuleURL       = "services/networking/networksecuritygroups/%s/rules/%s"
    26  
    27  	errParamNotSpecified = "Parameter %s is not specified."
    28  )
    29  
    30  // NewClient is used to instantiate a new SecurityGroupClient from an Azure client
    31  func NewClient(client management.Client) SecurityGroupClient {
    32  	return SecurityGroupClient{client: client}
    33  }
    34  
    35  // CreateNetworkSecurityGroup creates a new network security group within
    36  // the context of the specified subscription
    37  //
    38  // https://msdn.microsoft.com/en-us/library/azure/dn913818.aspx
    39  func (sg SecurityGroupClient) CreateNetworkSecurityGroup(
    40  	name string,
    41  	label string,
    42  	location string) (management.OperationID, error) {
    43  	if name == "" {
    44  		return "", fmt.Errorf(errParamNotSpecified, "name")
    45  	}
    46  	if location == "" {
    47  		return "", fmt.Errorf(errParamNotSpecified, "location")
    48  	}
    49  
    50  	data, err := xml.Marshal(SecurityGroupRequest{
    51  		Name:     name,
    52  		Label:    label,
    53  		Location: location,
    54  	})
    55  	if err != nil {
    56  		return "", err
    57  	}
    58  
    59  	requestURL := fmt.Sprintf(createSecurityGroupURL)
    60  	return sg.client.SendAzurePostRequest(requestURL, data)
    61  }
    62  
    63  // DeleteNetworkSecurityGroup deletes the specified network security group from the subscription
    64  //
    65  // https://msdn.microsoft.com/en-us/library/azure/dn913825.aspx
    66  func (sg SecurityGroupClient) DeleteNetworkSecurityGroup(
    67  	name string) (management.OperationID, error) {
    68  	if name == "" {
    69  		return "", fmt.Errorf(errParamNotSpecified, "name")
    70  	}
    71  
    72  	requestURL := fmt.Sprintf(deleteSecurityGroupURL, name)
    73  	return sg.client.SendAzureDeleteRequest(requestURL)
    74  }
    75  
    76  // GetNetworkSecurityGroup returns information about the specified network security group
    77  //
    78  // https://msdn.microsoft.com/en-us/library/azure/dn913821.aspx
    79  func (sg SecurityGroupClient) GetNetworkSecurityGroup(name string) (SecurityGroupResponse, error) {
    80  	if name == "" {
    81  		return SecurityGroupResponse{}, fmt.Errorf(errParamNotSpecified, "name")
    82  	}
    83  
    84  	var securityGroup SecurityGroupResponse
    85  
    86  	requestURL := fmt.Sprintf(getSecurityGroupURL, name)
    87  	response, err := sg.client.SendAzureGetRequest(requestURL)
    88  	if err != nil {
    89  		return securityGroup, err
    90  	}
    91  
    92  	err = xml.Unmarshal(response, &securityGroup)
    93  	return securityGroup, err
    94  }
    95  
    96  // ListNetworkSecurityGroups returns a list of the network security groups
    97  // in the specified subscription
    98  //
    99  // https://msdn.microsoft.com/en-us/library/azure/dn913815.aspx
   100  func (sg SecurityGroupClient) ListNetworkSecurityGroups() (SecurityGroupList, error) {
   101  	// the list of NetworkSecurityGroup items is wrapped in a NetworkSecurityGroups
   102  	// element so we need an outer struct representing this element.
   103  	type NetworkSecurityGroups struct {
   104  		SecurityGroupList SecurityGroupList `xml:"http://schemas.microsoft.com/windowsazure NetworkSecurityGroup"`
   105  	}
   106  	var securityGroups NetworkSecurityGroups
   107  
   108  	response, err := sg.client.SendAzureGetRequest(listSecurityGroupsURL)
   109  	if err != nil {
   110  		return securityGroups.SecurityGroupList, err
   111  	}
   112  
   113  	err = xml.Unmarshal(response, &securityGroups)
   114  	return securityGroups.SecurityGroupList, err
   115  }
   116  
   117  // AddNetworkSecurityToSubnet associates the network security group with
   118  // specified subnet in a virtual network
   119  //
   120  // https://msdn.microsoft.com/en-us/library/azure/dn913822.aspx
   121  func (sg SecurityGroupClient) AddNetworkSecurityToSubnet(
   122  	name string,
   123  	subnet string,
   124  	virtualNetwork string) (management.OperationID, error) {
   125  	if name == "" {
   126  		return "", fmt.Errorf(errParamNotSpecified, "name")
   127  	}
   128  	if subnet == "" {
   129  		return "", fmt.Errorf(errParamNotSpecified, "subnet")
   130  	}
   131  	if virtualNetwork == "" {
   132  		return "", fmt.Errorf(errParamNotSpecified, "virtualNetwork")
   133  	}
   134  
   135  	data, err := xml.Marshal(SecurityGroupRequest{Name: name})
   136  	if err != nil {
   137  		return "", err
   138  	}
   139  
   140  	requestURL := fmt.Sprintf(addSecurityGroupToSubnetURL, virtualNetwork, subnet)
   141  	return sg.client.SendAzurePostRequest(requestURL, data)
   142  }
   143  
   144  // GetNetworkSecurityGroupForSubnet returns information about the network
   145  // security group associated with a subnet
   146  //
   147  // https://msdn.microsoft.com/en-us/library/azure/dn913817.aspx
   148  func (sg SecurityGroupClient) GetNetworkSecurityGroupForSubnet(
   149  	subnet string,
   150  	virtualNetwork string) (SecurityGroupResponse, error) {
   151  	if subnet == "" {
   152  		return SecurityGroupResponse{}, fmt.Errorf(errParamNotSpecified, "subnet")
   153  	}
   154  	if virtualNetwork == "" {
   155  		return SecurityGroupResponse{}, fmt.Errorf(errParamNotSpecified, "virtualNetwork")
   156  	}
   157  
   158  	var securityGroup SecurityGroupResponse
   159  
   160  	requestURL := fmt.Sprintf(getSecurityGroupForSubnetURL, virtualNetwork, subnet)
   161  	response, err := sg.client.SendAzureGetRequest(requestURL)
   162  	if err != nil {
   163  		return securityGroup, err
   164  	}
   165  
   166  	err = xml.Unmarshal(response, &securityGroup)
   167  	return securityGroup, err
   168  }
   169  
   170  // RemoveNetworkSecurityGroupFromSubnet removes the association of the
   171  // specified network security group from the specified subnet
   172  //
   173  // https://msdn.microsoft.com/en-us/library/azure/dn913820.aspx
   174  func (sg SecurityGroupClient) RemoveNetworkSecurityGroupFromSubnet(
   175  	name string,
   176  	subnet string,
   177  	virtualNetwork string) (management.OperationID, error) {
   178  	if name == "" {
   179  		return "", fmt.Errorf(errParamNotSpecified, "name")
   180  	}
   181  	if subnet == "" {
   182  		return "", fmt.Errorf(errParamNotSpecified, "subnet")
   183  	}
   184  	if virtualNetwork == "" {
   185  		return "", fmt.Errorf(errParamNotSpecified, "virtualNetwork")
   186  	}
   187  
   188  	requestURL := fmt.Sprintf(removeSecurityGroupFromSubnetURL, virtualNetwork, subnet, name)
   189  	return sg.client.SendAzureDeleteRequest(requestURL)
   190  }
   191  
   192  // SetNetworkSecurityGroupRule adds or updates a network security rule that
   193  // is associated with the specified network security group
   194  //
   195  // https://msdn.microsoft.com/en-us/library/azure/dn913819.aspx
   196  func (sg SecurityGroupClient) SetNetworkSecurityGroupRule(
   197  	securityGroup string,
   198  	rule RuleRequest) (management.OperationID, error) {
   199  	if securityGroup == "" {
   200  		return "", fmt.Errorf(errParamNotSpecified, "securityGroup")
   201  	}
   202  	if rule.Name == "" {
   203  		return "", fmt.Errorf(errParamNotSpecified, "Name")
   204  	}
   205  	if rule.Type == "" {
   206  		return "", fmt.Errorf(errParamNotSpecified, "Type")
   207  	}
   208  	if rule.Priority == 0 {
   209  		return "", fmt.Errorf(errParamNotSpecified, "Priority")
   210  	}
   211  	if rule.Action == "" {
   212  		return "", fmt.Errorf(errParamNotSpecified, "Action")
   213  	}
   214  	if rule.SourceAddressPrefix == "" {
   215  		return "", fmt.Errorf(errParamNotSpecified, "SourceAddressPrefix")
   216  	}
   217  	if rule.SourcePortRange == "" {
   218  		return "", fmt.Errorf(errParamNotSpecified, "SourcePortRange")
   219  	}
   220  	if rule.DestinationAddressPrefix == "" {
   221  		return "", fmt.Errorf(errParamNotSpecified, "DestinationAddressPrefix")
   222  	}
   223  	if rule.DestinationPortRange == "" {
   224  		return "", fmt.Errorf(errParamNotSpecified, "DestinationPortRange")
   225  	}
   226  	if rule.Protocol == "" {
   227  		return "", fmt.Errorf(errParamNotSpecified, "Protocol")
   228  	}
   229  
   230  	data, err := xml.Marshal(rule)
   231  	if err != nil {
   232  		return "", err
   233  	}
   234  
   235  	requestURL := fmt.Sprintf(setSecurityGroupRuleURL, securityGroup, rule.Name)
   236  	return sg.client.SendAzurePutRequest(requestURL, "", data)
   237  }
   238  
   239  // DeleteNetworkSecurityGroupRule deletes a network security group rule from
   240  // the specified network security group
   241  //
   242  // https://msdn.microsoft.com/en-us/library/azure/dn913816.aspx
   243  func (sg SecurityGroupClient) DeleteNetworkSecurityGroupRule(
   244  	securityGroup string,
   245  	rule string) (management.OperationID, error) {
   246  	if securityGroup == "" {
   247  		return "", fmt.Errorf(errParamNotSpecified, "securityGroup")
   248  	}
   249  	if rule == "" {
   250  		return "", fmt.Errorf(errParamNotSpecified, "rule")
   251  	}
   252  
   253  	requestURL := fmt.Sprintf(deleteSecurityGroupRuleURL, securityGroup, rule)
   254  	return sg.client.SendAzureDeleteRequest(requestURL)
   255  }
   256  

View as plain text