Public/New-AGMLibGCEInstance.ps1

Function New-AGMLibGCEInstance ([string]$imageid,[string]$project,[string]$imagename,[string]$credentialid,[string]$zone,[string]$instancename,[string]$machinetype,[string]$serviceaccount,[string]$bootdisk,[string]$poweroffvm) 
{
    <#
    .SYNOPSIS
    Mounts a PD Snapshot as a new GCE Instance
    Get-AGMApplication -filtervalue "apptype=GCPInstance&managed=True"


    .EXAMPLE
    New-AGMLibGCEInstance -imageid 56410933 -credentialid 4447 -project testp2 -instancename avtest21 -machinetype e2-micro

    This mounts the specified imageid

    .DESCRIPTION
    A function to create a new GCE Instance from a PD Snapshot


    #>


    if ( (!($AGMSESSIONID)) -or (!($AGMIP)) )
    {
        Get-AGMErrorMessage -messagetoprint "Not logged in or session expired. Please login using Connect-AGM"
        return
    }
    else 
    {
        $sessiontest = (Get-AGMSession).session_id
        if ($sessiontest -ne $AGMSESSIONID)
        {
            Get-AGMErrorMessage -messagetoprint "Not logged in or session expired. Please login using Connect-AGM"
            return
        }
    }

    # learn about the image
    if (($imagename) -and (!($imageid)))
    {
        $imagecheck = Get-AGMImage -filtervalue backupname=$imagename
        if (!($imagecheck))
        {
            Get-AGMErrorMessage -messagetoprint "Failed to find $imagename using: Get-AGMImage -filtervalue backupname=$imagename"
            return
        }
        else 
        {
            $imagegrab = Get-AGMImage -id $imagecheck.id
            $imageid = $imagegrab.id
        }
    }
    if (($imageid) -and (!($imagename)))
    {
        $imagecheck = Get-AGMImage -id $imageid
        if (!($imagecheck))
        {
            Get-AGMErrorMessage -messagetoprint "Failed to find $imageid using: Get-AGMImage -id $imageid"
            return
        }
        else 
        {
            $imagename = $imagegrab.backupname
        }
    }
    if ((!($imagename)) -and (!($imageid)))
    {
        Get-AGMErrorMessage -messagetoprint "Please specify either -imageid or -imagename"
        return
    }

    if (!($credentialid))
    {
        Get-AGMErrorMessage -messagetoprint "Please specify a credential ID for the new instance with -credentialid"
        return
    }
    if (!($project))
    {
        Get-AGMErrorMessage -messagetoprint "Please specify a project for the new instance with -project"
        return
    }
    if (!($zone))
    {
        Get-AGMErrorMessage -messagetoprint "Please specify a zone for the new instance with -zone"
        return
    }
    if (!($instancename))
    {
        Get-AGMErrorMessage -messagetoprint "Please specify a name for the new instance with -instancename"
        return
    }
    if (!($machinetype))
    {
        Get-AGMErrorMessage -messagetoprint "Please specify a machinetype for the new instance with -machinetype"
        return
    }

    $json = ' {"@type":"mountRest","poweronvm":false,"migratevm":false,"cloudvmoptions":{"fields":[{"displayName":"","name":"cloudcredentials","helpId":1265,"type":"group","required":true,"modified":false,"children":[{"displayName":"CLOUD CREDENTIALS NAME","name":"cloudcredential","helpId":1265,"type":"sel
    ection","required":true,"modified":false,"dynamic":true,"choices":[{"displayName":"avwlab2","name":"4447","selected":true}],"_getchoices":"getCloudCredentials#cloudcredentiallist,image","_dependent":["project","zone","machinetype","vpc","subnet","privateips","externalip"],"_default":"4447"},{
    "displayName":"PROJECT NAME","name":"project","helpId":1265,"type":"selection","required":true,"modified":false,"dynamic":true,"choices":[{"displayName":"avwlab2","name":"avwlab2","selected":true}],"_getchoices":"getAllProjects#handle,cloudcredential","_dependent":["zone","machinetype","vpc",
    "subnet","privateips","externalip"],"_default":"avwlab2"},{"displayName":"ZONE","name":"zone","helpId":1265,"type":"selection","required":true,"modified":false,"dynamic":true,"choices":[{"displayName":"australia-southeast1-c","name":"australia-southeast1-c","selected":true}],"_getchoices":"ge
    tZones#handle,cloudcredential,region","_dependent":["machinetype","vpc","subnet","privateips","externalip"],"_default":"australia-southeast1-c"}],"groupType":"layout"},{"displayName":"","name":"instancesettings","helpId":1265,"type":"group","required":true,"modified":true,"children":[{"displa
    yName":"INSTANCE NAME","name":"instancename","helpId":1265,"type":"text","required":true,"currentValue":"avtest","modified":true,"size":40,"_getDefault":"getDefaultInstanceName","_default":"test"},{"displayName":"MACHINE
    TYPE","name":"machinetype","helpId":1265,"type":"selection","required":true,"modified":false,"dynamic":true,"choices":[{"displayName":"e2-highcpu-2(2 vCPU, 2.00GB
    memory)","name":"e2-highcpu-2","selected":true}],"_getchoices":"getMachineTypes#handle,cloudcredential,region,project,zone","_dependent":["networksettings"],"_default":"e2-micro"},{"displayName":"SERVICE
    ACCOUNT","name":"serviceaccount","helpId":1265,"type":"text","required":true,"currentValue":"avwlabowner@avwlab2.iam.gserviceaccount.com","modified":false,"size":40,"_getDefault":"getDefaultServiceAccount","_default":"avwlabowner@avwlab2.iam.gserviceaccount.com"},{"displayName":"NETWORK TAGS"
    ,"name":"networktag","helpId":1265,"type":"multiselection","required":false,"modified":false,"minimum":0,"maximum":10,"_getchoices":"getDefaultNetworkTags","_default":"[]"}],"groupType":"layout"},{"displayName":"","name":"tagsgroup","helpId":1265,"type":"group","required":true,"modified":true
    ,"children":[{"displayName":"Labels","name":"tag","helpId":1265,"type":"tagselection","modified":true,"minimum":1,"maximum":50,"choices":[{"selected":false,"key":"anthony","value":"testme"}],"validation":""}],"groupType":"layout"},{"displayName":"Network Interfaces","name":"networksettings","
    helpId":1265,"type":"networkinterfaces","required":true,"modified":true,"minimum":1,"maximum":2,"children":[{"displayName":"nic0","name":"nic0","helpId":1265,"type":"nic","required":true,"modified":true,"children":[{"displayName":"NETWORK","name":"vpc","helpId":1265,"type":"selection","requir
    ed":true,"modified":false,"dynamic":true,"choices":[{"displayName":"default (default)","name":"https://www.googleapis.com/compute/v1/projects/avwlab2/global/networks/default","selected":true}],"_getchoices":"getVpcs#handle,cloudcredential,region,project,resourcegroup,zone","_dependent":["subn
    et","privateips"],"_default":"https://www.googleapis.com/compute/v1/projects/avwlab2/global/networks/default"},{"displayName":"SUBNET","name":"subnet","helpId":1265,"type":"selection","required":true,"modified":false,"dynamic":true,"choices":[{"displayName":"default (default)","name":"https:/
    /www.googleapis.com/compute/v1/projects/avwlab2/regions/australia-southeast1/subnetworks/default","selected":true}],"_getchoices":"getSubnets#handle,cloudcredential,region,vpc,project,zone","_dependent":["privateips"],"_default":"https://www.googleapis.com/compute/v1/projects/avwlab2/regions/
    australia-southeast1/subnetworks/default"},{"displayName":"EXTERNAL IPV4 IP","name":"externalip","helpId":1265,"type":"selection","required":true,"modified":true,"choices":[{"displayName":"Auto
    Assign","name":"0.0.0.0","selected":true},{"displayName":"None","name":"None"}],"_getchoices":"getElasticIPs#handle,project,region,zone,cloudcredential","_default":"None"},{"displayName":"INTERNAL IPV4
    IP","name":"internalip","helpId":1265,"type":"internalipaddress","modified":true,"children":[{"displayName":"Auto Assign","name":"internalipassign","helpId":1265,"type":"radiobutton","modified":false,"checked":false},{"displayName":"Manual Assign","name":"internalipassign","helpId":1265,"type
    ":"radiobutton","modified":true,"checked":true},{"name":"privateips","helpId":1265,"type":"ipaddress","modified":true,"minimum":1,"maximum":2,"_getchoices":"getCIDRBlock#handle,cloudcredential,zone,project,vpc,subnet","values":["10.152.0.10"],"disabled":true,"validation":"10.152.0.0/20"}]}]}]
    ,"groupType":"layout"},{"displayName":"","name":"volumes","helpId":1265,"type":"group","required":true,"modified":false,"children":[{"displayName":"Select Volumes to Mount","name":"volumeselection","helpId":1265,"type":"volumeselectiontable","required":true,"modified":false,"title":"Select
    Volumes to Mount","header":[{"displayName":"DEVICE NAME","name":"devicename","type":"text"},{"displayName":"SIZE","name":"size","type":"text"},{"displayName":"SOURCE MOUNT PATH","name":"sourcemountpath","type":"text"},{"displayName":"DEVICE
    INDEX","name":"deviceindex","type":"text"},{"displayName":"VOLUME ID","name":"volumeid","type":"text"},{"displayName":"DEVICE TYPE","name":"devicetype","type":"text"},{"displayName":"DISK TYPE","name":"disktype","type":"selection"}],"rows":[{"size":"50
    GB","volumeid":"4946607829063451857","devicename":"test","disktype":[{"displayName":"Balanced Persistent Disk(DiskSizeGb: 100)","name":"pd-balanced","selected":true},{"displayName":"SSD Persistent Disk(DiskSizeGb: 100)","name":"pd-ssd"},{"displayName":"Standard Persistent Disk(DiskSizeGb: 500
    )","name":"pd-standard","selected":true}],"sourcemountpath":"dev/sda","devicetype":"BOOT","deviceindex":"0","selected":true,"disabled":true,"disktypeDisabled":false}],"_getchoices":"getVolumeTypes#handle,cloudcredential,region,zone,project,machinetype"}],"groupType":"wizard"},{"displayName":"
    ","name":"checkboxgroup","helpId":1265,"type":"group","modified":false,"children":[{"displayName":"Power On","name":"poweronvm","helpId":1265,"type":"checkbox","modified":false,"checked":true}],"groupType":"layout"}],"formtype":"newmount","image":"Image_0018442","cloudtype":"GCP"}}'


        
    Post-AGMAPIData  -endpoint /backup/$imageid/mount -body $json

    $json = '{"cloudvmoptions":{"@type":"cloudVmMountRest","fields":['
    $json = $json + '{"displayName":"test","name":"cloudcredentials","helpId":1265,"type":"group","required":true,"modified":false,"children":['
    $json = $json + '{"displayName":"CLOUD CREDENTIALS NAME","name":"cloudcredential","helpId":1265,"type":"selection","required":true,"modified":false,"dynamic":true,"choices":['
    $json = $json + '{"displayName":"test","name":"' + $credentialid +'","selected":true}],"_getchoices":"getCloudCredentials#cloudcredentiallist,image","_dependent":["project","zone","machinetype","vpc","subnet","privateips","externalip"],"_default":"1234"},'
    $json = $json + '{"displayName":"PROJECT NAME","name":"project","helpId":1265,"type":"selection","required":true,"modified":false,"dynamic":true,"choices":['
    $json = $json + '{"displayName":"test","name":"' + $project + '","selected":true}],"_getchoices":"getAllProjects#handle,cloudcredential","_dependent":["zone","machinetype","vpc","subnet","privateips","externalip"],"_default":"test"},'
    $json = $json + '{"displayName":"ZONE","name":"zone","helpId":1265,"type":"selection","required":true,"modified":false,"dynamic":true,"choices":['
    $json = $json + '{"displayName":"test","name":"' + $zone + '","selected":true}],"_getchoices":"getZones#handle,cloudcredential,region","_dependent":["machinetype","vpc","subnet","privateips","externalip"],"_default":"test"}],"groupType":"layout"},'
    $json = $json + '{"displayName":"test","name":"instancesettings","helpId":1265,"type":"group","required":true,"modified":true,"children":['
    $json = $json + '{"displayName":"INSTANCE NAME","name":"instancename","helpId":1265,"type":"text","required":true,"currentValue":"' + $instancename
    $json = $json + '","modified":true,"size":40,"_getDefault":"getDefaultInstanceName","_default":"test"},'
    $json = $json + '{"displayName":"MACHINE TYPE","name":"machinetype","helpId":1265,"type":"selection","required":true,"modified":false,"dynamic":true,"choices":['
    $json = $json + '{"displayName":"test","name":"' + $machinetype + '","selected":true}],"_getchoices":"getMachineTypes#handle,cloudcredential,region,project,zone","_dependent":["networksettings"],"_default":"e2-micro"},'
    $json = $json + '{"displayName":"SERVICE ACCOUNT","name":"serviceaccount","helpId":1265,"type":"text","required":true,"currentValue":"85163945504-compute@developer.gserviceaccount.com","modified":false,"size":40,"_getDefault":"getDefaultServiceAccount","_default":"test"},'
    $json = $json + '{"displayName":"NETWORK TAGS","name":"networktag","helpId":1265,"type":"multiselection","required":false,"modified":false,"minimum":0,"maximum":10,"_getchoices":"getDefaultNetworkTags","_default":"[]"}],"groupType":"layout"},'
    $json = $json + '{"displayName":"test","name":"tagsgroup","helpId":1265,"type":"group","required":true,"modified":true,"children":['
    $json = $json + '{"displayName":"Labels","name":"tag","helpId":1265,"type":"tagselection","modified":true,"minimum":1,"maximum":50,"choices":[{"selected":false,"key":"anthony","value":"testme"}],"validation":""}],"groupType":"layout"},'
    $json = $json + '{"displayName":"Network Interfaces","name":"networksettings","helpId":1265,"type":"networkinterfaces","required":true,"modified":true,"minimum":1,"maximum":2,"children":['
    $json = $json + '{"displayName":"nic0","name":"nic0","helpId":1265,"type":"nic","required":true,"modified":true,"children":['
    $json = $json + '{"displayName":"NETWORK","name":"vpc","helpId":1265,"type":"selection","required":true,"modified":false,"dynamic":true,"choices":['
    $json = $json + '{"displayName":"default (default)","name":"https://www.googleapis.com/compute/v1/projects/avwlab2/global/networks/default","selected":true}],"_getchoices":"getVpcs#handle,cloudcredential,region,project,resourcegroup,zone","_dependent":["subnet","privateips"],"_default":"https://www.googleapis.com/compute/v1/projects/avwlab2/global/networks/default","parentName":"nic0"},'
    $json = $json + '{"displayName":"SUBNET","name":"subnet","helpId":1265,"type":"selection","required":true,"modified":false,"dynamic":true,"choices":['
    $json = $json + '{"displayName":"default (default)","name":"https://www.googleapis.com/compute/v1/projects/avwlab2/regions/australia-southeast1/subnetworks/default","selected":true}],"_getchoices":"getSubnets#handle,cloudcredential,region,vpc,project,zone","_dependent":["privateips"],"_default":"https://www.googleapis.com/compute/v1/projects/avwlab2/regions/australia-southeast1/subnetworks/default","parentName":"nic0"},'
    $json = $json + '{"displayName":"EXTERNAL IPV4 IP","name":"externalip","helpId":1265,"type":"selection","required":true,"modified":true,"choices":['
    $json = $json + '{"displayName":"Auto Assign","name":"0.0.0.0","selected":true},'
    $json = $json + '{"displayName":"None","name":"None"}],"_getchoices":"getElasticIPs#handle,project,region,zone,cloudcredential","_default":"None","parentName":"nic0"},'
    $json = $json + '{"displayName":"INTERNAL IPV4 IP","name":"internalip","helpId":1265,"type":"internalipaddress","modified":true,"children":['
    $json = $json + '{"displayName":"Auto Assign","name":"internalipassign","helpId":1265,"type":"radiobutton","modified":false,"checked":false,"parentName":"nic0"},'
    $json = $json + '{"displayName":"Manual Assign","name":"internalipassign","helpId":1265,"type":"radiobutton","modified":true,"checked":true,"parentName":"nic0"},{"name":"privateips","helpId":1265,"type":"ipaddress","modified":true,"minimum":1,"maximum":2,"_getchoices":"getCIDRBlock#handle,cloudcredential,zone,project,vpc,subnet","values":["10.152.0.10"],"disabled":true,"validation":"10.152.0.0/20","parentName":"nic0"}],"parentName":"nic0"}]}],"groupType":"layout"},'
    $json = $json + '{"displayName":"test","name":"volumes","helpId":1265,"type":"group","required":true,"modified":false,"children":['
    $json = $json + '{"displayName":"Select Volumes to Mount","name":"volumeselection","helpId":1265,"type":"volumeselectiontable","required":true,"modified":false,"title":"Select Volumes to Mount","header":['
    $json = $json + '{"displayName":"DEVICE NAME","name":"devicename","type":"text"},'
    $json = $json + '{"displayName":"SIZE","name":"size","type":"text"},'
    $json = $json + '{"displayName":"SOURCE MOUNT PATH","name":"sourcemountpath","type":"text"},'
    $json = $json + '{"displayName":"DEVICE INDEX","name":"deviceindex","type":"text"},'
    $json = $json + '{"displayName":"VOLUME ID","name":"volumeid","type":"text"},'
    $json = $json + '{"displayName":"DEVICE TYPE","name":"devicetype","type":"text"},'
    $json = $json + '{"displayName":"DISK TYPE","name":"disktype","type":"selection"}],"rows":[{"size":"50 GB","volumeid":"4946607829063451857","devicename":"test","disktype":['
    $json = $json + '{"displayName":"Balanced Persistent Disk(DiskSizeGb: 100)","name":"pd-balanced","selected":true},'
    $json = $json + '{"displayName":"SSD Persistent Disk(DiskSizeGb: 100)","name":"pd-ssd"},'
    $json = $json + '{"displayName":"Standard Persistent Disk(DiskSizeGb: 500)","name":"pd-standard","selected":true}],"sourcemountpath":"dev/sda","devicetype":"BOOT","deviceindex":"0","selected":true,"disabled":true,"disktypeDisabled":false}],"_getchoices":"getVolumeTypes#handle,cloudcredential,region,zone,project,machinetype"}],"groupType":"wizard"},'
    $json = $json + '{"displayName":"test","name":"checkboxgroup","helpId":1265,"type":"group","modified":false,"children":['
    $json = $json + '{"displayName":"Power On","name":"poweronvm","helpId":1265,"type":"checkbox","modified":false,"checked":true}],"groupType":"layout"}],"formtype":"newmount","image":"'
    # insert imagename into JSON
    $json = $json + $imagename
    $json = $json + '","cloudtype":"GCP"}}'
    
    
}