github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/virtual_machine.go (about) 1 // Copyright 2019 The Terraformer Authors. 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 "context" 19 "log" 20 21 "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-03-01/compute" 22 "github.com/Azure/go-autorest/autorest" 23 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 24 "github.com/hashicorp/go-azure-helpers/authentication" 25 ) 26 27 type VirtualMachineGenerator struct { 28 AzureService 29 } 30 31 func (g VirtualMachineGenerator) createResources(virtualMachineListResultIterator compute.VirtualMachineListResultIterator) ([]terraformutils.Resource, error) { 32 var resources []terraformutils.Resource 33 for virtualMachineListResultIterator.NotDone() { 34 vm := virtualMachineListResultIterator.Value() 35 var newResource terraformutils.Resource 36 if vm.VirtualMachineProperties.OsProfile == nil { 37 if vm.VirtualMachineProperties.StorageProfile.OsDisk.OsType == "Windows" { 38 newResource = terraformutils.NewSimpleResource( 39 *vm.ID, 40 *vm.Name, 41 "azurerm_windows_virtual_machine", 42 "azurerm", 43 []string{}) 44 } else { 45 newResource = terraformutils.NewSimpleResource( 46 *vm.ID, 47 *vm.Name, 48 "azurerm_linux_virtual_machine", 49 "azurerm", 50 []string{}) 51 } 52 } else { 53 if vm.VirtualMachineProperties.OsProfile.WindowsConfiguration != nil { 54 newResource = terraformutils.NewSimpleResource( 55 *vm.ID, 56 *vm.Name, 57 "azurerm_windows_virtual_machine", 58 "azurerm", 59 []string{}) 60 } else { 61 newResource = terraformutils.NewSimpleResource( 62 *vm.ID, 63 *vm.Name, 64 "azurerm_linux_virtual_machine", 65 "azurerm", 66 []string{}) 67 } 68 } 69 70 resources = append(resources, newResource) 71 if err := virtualMachineListResultIterator.Next(); err != nil { 72 log.Println(err) 73 return resources, err 74 } 75 } 76 return resources, nil 77 } 78 79 func (g *VirtualMachineGenerator) InitResources() error { 80 ctx := context.Background() 81 vmClient := compute.NewVirtualMachinesClient(g.Args["config"].(authentication.Config).SubscriptionID) 82 83 vmClient.Authorizer = g.Args["authorizer"].(autorest.Authorizer) 84 85 var ( 86 output compute.VirtualMachineListResultIterator 87 err error 88 ) 89 if rg := g.Args["resource_group"].(string); rg != "" { 90 output, err = vmClient.ListComplete(ctx, rg) 91 } else { 92 output, err = vmClient.ListAllComplete(ctx) 93 } 94 if err != nil { 95 return err 96 } 97 g.Resources, err = g.createResources(output) 98 return err 99 }