2022-03-22 11:03:15 -04:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
|
2022-07-27 16:02:33 -04:00
|
|
|
armcomputev2 "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v2"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ScaleSet defines a Azure scale set.
|
|
|
|
type ScaleSet struct {
|
2022-05-24 04:04:42 -04:00
|
|
|
Name string
|
|
|
|
NamePrefix string
|
|
|
|
Subscription string
|
|
|
|
ResourceGroup string
|
|
|
|
Location string
|
|
|
|
InstanceType string
|
|
|
|
StateDiskSizeGB int32
|
2022-08-02 06:24:55 -04:00
|
|
|
StateDiskType string
|
2022-05-24 04:04:42 -04:00
|
|
|
Count int64
|
|
|
|
Username string
|
|
|
|
SubnetID string
|
|
|
|
NetworkSecurityGroup string
|
|
|
|
Password string
|
|
|
|
Image string
|
|
|
|
UserAssignedIdentity string
|
|
|
|
LoadBalancerName string
|
|
|
|
LoadBalancerBackendAddressPool string
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Azure returns the Azure representation of ScaleSet.
|
2022-07-27 16:02:33 -04:00
|
|
|
func (s ScaleSet) Azure() armcomputev2.VirtualMachineScaleSet {
|
|
|
|
return armcomputev2.VirtualMachineScaleSet{
|
|
|
|
Name: to.Ptr(s.Name),
|
|
|
|
Location: to.Ptr(s.Location),
|
|
|
|
SKU: &armcomputev2.SKU{
|
|
|
|
Name: to.Ptr(s.InstanceType),
|
|
|
|
Capacity: to.Ptr(s.Count),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
Properties: &armcomputev2.VirtualMachineScaleSetProperties{
|
|
|
|
Overprovision: to.Ptr(false),
|
|
|
|
UpgradePolicy: &armcomputev2.UpgradePolicy{
|
|
|
|
Mode: to.Ptr(armcomputev2.UpgradeModeManual),
|
|
|
|
AutomaticOSUpgradePolicy: &armcomputev2.AutomaticOSUpgradePolicy{
|
|
|
|
EnableAutomaticOSUpgrade: to.Ptr(false),
|
|
|
|
DisableAutomaticRollback: to.Ptr(false),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
VirtualMachineProfile: &armcomputev2.VirtualMachineScaleSetVMProfile{
|
|
|
|
OSProfile: &armcomputev2.VirtualMachineScaleSetOSProfile{
|
|
|
|
ComputerNamePrefix: to.Ptr(s.NamePrefix),
|
|
|
|
AdminUsername: to.Ptr(s.Username),
|
|
|
|
AdminPassword: to.Ptr(s.Password),
|
|
|
|
LinuxConfiguration: &armcomputev2.LinuxConfiguration{},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
StorageProfile: &armcomputev2.VirtualMachineScaleSetStorageProfile{
|
|
|
|
ImageReference: &armcomputev2.ImageReference{
|
|
|
|
ID: to.Ptr(s.Image),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
DataDisks: []*armcomputev2.VirtualMachineScaleSetDataDisk{
|
2022-04-05 08:25:49 -04:00
|
|
|
{
|
2022-07-27 16:02:33 -04:00
|
|
|
CreateOption: to.Ptr(armcomputev2.DiskCreateOptionTypesEmpty),
|
|
|
|
DiskSizeGB: to.Ptr(s.StateDiskSizeGB),
|
|
|
|
Lun: to.Ptr[int32](0),
|
|
|
|
ManagedDisk: &armcomputev2.VirtualMachineScaleSetManagedDiskParameters{
|
|
|
|
StorageAccountType: (*armcomputev2.StorageAccountTypes)(to.Ptr(s.StateDiskType)),
|
2022-08-02 06:24:55 -04:00
|
|
|
},
|
2022-04-05 08:25:49 -04:00
|
|
|
},
|
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
NetworkProfile: &armcomputev2.VirtualMachineScaleSetNetworkProfile{
|
|
|
|
NetworkInterfaceConfigurations: []*armcomputev2.VirtualMachineScaleSetNetworkConfiguration{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-07-27 16:02:33 -04:00
|
|
|
Name: to.Ptr(s.Name),
|
|
|
|
Properties: &armcomputev2.VirtualMachineScaleSetNetworkConfigurationProperties{
|
|
|
|
Primary: to.Ptr(true),
|
|
|
|
EnableIPForwarding: to.Ptr(true),
|
|
|
|
IPConfigurations: []*armcomputev2.VirtualMachineScaleSetIPConfiguration{
|
2022-03-22 11:03:15 -04:00
|
|
|
{
|
2022-07-27 16:02:33 -04:00
|
|
|
Name: to.Ptr(s.Name),
|
|
|
|
Properties: &armcomputev2.VirtualMachineScaleSetIPConfigurationProperties{
|
|
|
|
Primary: to.Ptr(true),
|
|
|
|
Subnet: &armcomputev2.APIEntityReference{
|
|
|
|
ID: to.Ptr(s.SubnetID),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
LoadBalancerBackendAddressPools: []*armcomputev2.SubResource{
|
2022-05-24 04:04:42 -04:00
|
|
|
{
|
2022-07-27 16:02:33 -04:00
|
|
|
ID: to.Ptr("/subscriptions/" + s.Subscription + "/resourcegroups/" + s.ResourceGroup + "/providers/Microsoft.Network/loadBalancers/" + s.LoadBalancerName + "/backendAddressPools/" + s.LoadBalancerBackendAddressPool),
|
2022-05-24 04:04:42 -04:00
|
|
|
},
|
|
|
|
{
|
2022-07-27 16:02:33 -04:00
|
|
|
ID: to.Ptr("/subscriptions/" + s.Subscription + "/resourcegroups/" + s.ResourceGroup + "/providers/Microsoft.Network/loadBalancers/" + s.LoadBalancerName + "/backendAddressPools/all"),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
NetworkSecurityGroup: &armcomputev2.SubResource{
|
|
|
|
ID: to.Ptr(s.NetworkSecurityGroup),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
SecurityProfile: &armcomputev2.SecurityProfile{
|
|
|
|
SecurityType: to.Ptr(armcomputev2.SecurityTypesTrustedLaunch),
|
|
|
|
UefiSettings: &armcomputev2.UefiSettings{VTpmEnabled: to.Ptr(true)},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
DiagnosticsProfile: &armcomputev2.DiagnosticsProfile{
|
|
|
|
BootDiagnostics: &armcomputev2.BootDiagnostics{
|
|
|
|
Enabled: to.Ptr(true),
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-07-27 16:02:33 -04:00
|
|
|
Identity: &armcomputev2.VirtualMachineScaleSetIdentity{
|
|
|
|
Type: to.Ptr(armcomputev2.ResourceIdentityTypeUserAssigned),
|
|
|
|
UserAssignedIdentities: map[string]*armcomputev2.UserAssignedIdentitiesValue{
|
2022-03-22 11:03:15 -04:00
|
|
|
s.UserAssignedIdentity: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GeneratePassword is a helper function to generate a random password
|
|
|
|
// for Azure's scale set.
|
|
|
|
func GeneratePassword() (string, error) {
|
|
|
|
letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
|
|
|
|
|
|
pwLen := 16
|
|
|
|
pw := make([]byte, 0, pwLen)
|
|
|
|
for i := 0; i < pwLen; i++ {
|
|
|
|
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
pw = append(pw, letters[n.Int64()])
|
|
|
|
}
|
|
|
|
// bypass password rules
|
|
|
|
pw = append(pw, []byte("Aa1!")...)
|
|
|
|
return string(pw), nil
|
|
|
|
}
|