github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/azure/management_lock.go (about) 1 // Copyright 2021 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/resources/mgmt/2016-09-01/locks" 22 ) 23 24 type ManagementLockGenerator struct { 25 AzureService 26 } 27 28 func (az *ManagementLockGenerator) listResources() ([]locks.ManagementLockObject, error) { 29 subscriptionID, resourceGroup, authorizer := az.getClientArgs() 30 client := locks.NewManagementLocksClient(subscriptionID) 31 client.Authorizer = authorizer 32 var ( 33 iterator locks.ManagementLockListResultIterator 34 err error 35 ) 36 ctx := context.Background() 37 if resourceGroup != "" { 38 iterator, err = client.ListAtResourceGroupLevelComplete(ctx, resourceGroup, "") 39 } else { 40 iterator, err = client.ListAtSubscriptionLevelComplete(ctx, "") 41 } 42 if err != nil { 43 return nil, err 44 } 45 var resources []locks.ManagementLockObject 46 for iterator.NotDone() { 47 item := iterator.Value() 48 resources = append(resources, item) 49 if err := iterator.NextWithContext(ctx); err != nil { 50 log.Println(err) 51 return resources, err 52 } 53 } 54 return resources, nil 55 } 56 57 func (az *ManagementLockGenerator) appendResource(resource *locks.ManagementLockObject) { 58 az.AppendSimpleResource(*resource.ID, *resource.Name, "azurerm_management_lock") 59 } 60 61 func (az *ManagementLockGenerator) InitResources() error { 62 63 resources, err := az.listResources() 64 if err != nil { 65 return err 66 } 67 for _, resource := range resources { 68 az.appendResource(&resource) 69 } 70 return nil 71 }