...

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

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

     1  // +build go1.7
     2  
     3  package vmutils
     4  
     5  // Copyright (c) Microsoft Corporation. All rights reserved.
     6  // Licensed under the MIT License. See License.txt in the project root for license information.
     7  
     8  import (
     9  	"fmt"
    10  
    11  	"github.com/Azure/azure-sdk-for-go/services/classic/management"
    12  	lc "github.com/Azure/azure-sdk-for-go/services/classic/management/location"
    13  	vm "github.com/Azure/azure-sdk-for-go/services/classic/management/virtualmachine"
    14  )
    15  
    16  // IsRoleSizeValid retrieves the available rolesizes using
    17  // vmclient.GetRoleSizeList() and returns whether that the provided roleSizeName
    18  // is part of that list
    19  func IsRoleSizeValid(vmclient vm.VirtualMachineClient, roleSizeName string) (bool, error) {
    20  	if roleSizeName == "" {
    21  		return false, fmt.Errorf(errParamNotSpecified, "roleSizeName")
    22  	}
    23  
    24  	roleSizeList, err := vmclient.GetRoleSizeList()
    25  	if err != nil {
    26  		return false, err
    27  	}
    28  
    29  	for _, roleSize := range roleSizeList.RoleSizes {
    30  		if roleSize.Name == roleSizeName {
    31  			return true, nil
    32  		}
    33  	}
    34  
    35  	return false, nil
    36  }
    37  
    38  // IsRoleSizeAvailableInLocation retrieves all available sizes in the specified
    39  // location and returns whether that the provided roleSizeName is part of that list.
    40  func IsRoleSizeAvailableInLocation(managementclient management.Client, location, roleSizeName string) (bool, error) {
    41  	if location == "" {
    42  		return false, fmt.Errorf(errParamNotSpecified, "location")
    43  	}
    44  	if roleSizeName == "" {
    45  		return false, fmt.Errorf(errParamNotSpecified, "roleSizeName")
    46  	}
    47  
    48  	locationClient := lc.NewClient(managementclient)
    49  	locationInfo, err := getLocation(locationClient, location)
    50  	if err != nil {
    51  		return false, err
    52  	}
    53  
    54  	for _, availableRoleSize := range locationInfo.VirtualMachineRoleSizes {
    55  		if availableRoleSize == roleSizeName {
    56  			return true, nil
    57  		}
    58  	}
    59  
    60  	return false, nil
    61  }
    62  
    63  func getLocation(c lc.LocationClient, location string) (*lc.Location, error) {
    64  	if location == "" {
    65  		return nil, fmt.Errorf(errParamNotSpecified, "location")
    66  	}
    67  
    68  	locations, err := c.ListLocations()
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	for _, existingLocation := range locations.Locations {
    74  		if existingLocation.Name != location {
    75  			continue
    76  		}
    77  
    78  		return &existingLocation, nil
    79  	}
    80  	return nil, fmt.Errorf("Invalid location: %s. Available locations: %s", location, locations)
    81  }
    82  

View as plain text