github.com/coreos/mantle@v0.13.0/platform/api/azure/network.go (about)

     1  // Copyright 2018 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package azure
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/Azure/azure-sdk-for-go/arm/network"
    21  
    22  	"github.com/coreos/mantle/util"
    23  )
    24  
    25  var (
    26  	virtualNetworkPrefix = []string{"10.0.0.0/16"}
    27  	subnetPrefix         = "10.0.0.0/24"
    28  )
    29  
    30  func (a *API) PrepareNetworkResources(resourceGroup string) (network.Subnet, error) {
    31  	if err := a.createVirtualNetwork(resourceGroup); err != nil {
    32  		return network.Subnet{}, err
    33  	}
    34  
    35  	return a.createSubnet(resourceGroup)
    36  }
    37  
    38  func (a *API) createVirtualNetwork(resourceGroup string) error {
    39  	_, err := a.netClient.CreateOrUpdate(resourceGroup, "kola-vn", network.VirtualNetwork{
    40  		Location: &a.opts.Location,
    41  		VirtualNetworkPropertiesFormat: &network.VirtualNetworkPropertiesFormat{
    42  			AddressSpace: &network.AddressSpace{
    43  				AddressPrefixes: &virtualNetworkPrefix,
    44  			},
    45  		},
    46  	}, nil)
    47  
    48  	return err
    49  }
    50  
    51  func (a *API) createSubnet(resourceGroup string) (network.Subnet, error) {
    52  	_, err := a.subClient.CreateOrUpdate(resourceGroup, "kola-vn", "kola-subnet", network.Subnet{
    53  		SubnetPropertiesFormat: &network.SubnetPropertiesFormat{
    54  			AddressPrefix: &subnetPrefix,
    55  		},
    56  	}, nil)
    57  	if err != nil {
    58  		return network.Subnet{}, err
    59  	}
    60  
    61  	return a.getSubnet(resourceGroup)
    62  }
    63  
    64  func (a *API) getSubnet(resourceGroup string) (network.Subnet, error) {
    65  	return a.subClient.Get(resourceGroup, "kola-vn", "kola-subnet", "")
    66  }
    67  
    68  func (a *API) createPublicIP(resourceGroup string) (*network.PublicIPAddress, error) {
    69  	name := randomName("ip")
    70  
    71  	_, err := a.ipClient.CreateOrUpdate(resourceGroup, name, network.PublicIPAddress{
    72  		Location: &a.opts.Location,
    73  	}, nil)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	ip, err := a.ipClient.Get(resourceGroup, name, "")
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	return &ip, nil
    84  }
    85  
    86  func (a *API) GetPublicIP(name, resourceGroup string) (string, error) {
    87  	ip, err := a.ipClient.Get(resourceGroup, name, "")
    88  	if err != nil {
    89  		return "", err
    90  	}
    91  
    92  	if ip.PublicIPAddressPropertiesFormat.IPAddress == nil {
    93  		return "", fmt.Errorf("IP Address is nil")
    94  	}
    95  
    96  	return *ip.PublicIPAddressPropertiesFormat.IPAddress, nil
    97  }
    98  
    99  // returns PublicIP, PrivateIP, error
   100  func (a *API) GetIPAddresses(name, publicIPName, resourceGroup string) (string, string, error) {
   101  	publicIP, err := a.GetPublicIP(publicIPName, resourceGroup)
   102  	if err != nil {
   103  		return "", "", err
   104  	}
   105  
   106  	nic, err := a.intClient.Get(resourceGroup, name, "")
   107  	if err != nil {
   108  		return "", "", err
   109  	}
   110  
   111  	configs := *nic.InterfacePropertiesFormat.IPConfigurations
   112  	for _, conf := range configs {
   113  		if conf.PrivateIPAddress == nil {
   114  			return "", "", fmt.Errorf("PrivateIPAddress is nil")
   115  		}
   116  		return publicIP, *conf.PrivateIPAddress, nil
   117  	}
   118  	return "", "", fmt.Errorf("no ip configurations found")
   119  }
   120  
   121  func (a *API) GetPrivateIP(name, resourceGroup string) (string, error) {
   122  	nic, err := a.intClient.Get(resourceGroup, name, "")
   123  	if err != nil {
   124  		return "", err
   125  	}
   126  
   127  	configs := *nic.InterfacePropertiesFormat.IPConfigurations
   128  	return *configs[0].PrivateIPAddress, nil
   129  }
   130  
   131  func (a *API) createNIC(ip *network.PublicIPAddress, subnet *network.Subnet, resourceGroup string) (*network.Interface, error) {
   132  	name := randomName("nic")
   133  	ipconf := randomName("nic-ipconf")
   134  
   135  	_, err := a.intClient.CreateOrUpdate(resourceGroup, name, network.Interface{
   136  		Location: &a.opts.Location,
   137  		InterfacePropertiesFormat: &network.InterfacePropertiesFormat{
   138  			IPConfigurations: &[]network.InterfaceIPConfiguration{
   139  				{
   140  					Name: &ipconf,
   141  					InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
   142  						PublicIPAddress:           ip,
   143  						PrivateIPAllocationMethod: network.Dynamic,
   144  						Subnet:                    subnet,
   145  					},
   146  				},
   147  			},
   148  			EnableAcceleratedNetworking: util.BoolToPtr(true),
   149  		},
   150  	}, nil)
   151  	if err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	nic, err := a.intClient.Get(resourceGroup, name, "")
   156  	if err != nil {
   157  		return nil, err
   158  	}
   159  
   160  	return &nic, nil
   161  }