github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/iot.go (about) 1 // Copyright 2020 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 aws 16 17 import ( 18 "context" 19 20 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 21 "github.com/aws/aws-sdk-go-v2/service/iot" 22 ) 23 24 var iotAllowEmptyValues = []string{"tags."} 25 26 type IotGenerator struct { 27 AWSService 28 } 29 30 func (g *IotGenerator) InitResources() error { 31 config, e := g.generateConfig() 32 if e != nil { 33 return e 34 } 35 svc := iot.NewFromConfig(config) 36 37 if err := g.loadThingTypes(svc); err != nil { 38 return err 39 } 40 if err := g.loadThings(svc); err != nil { 41 return err 42 } 43 if err := g.loadTopicRules(svc); err != nil { 44 return err 45 } 46 if err := g.loadRoleAliases(svc); err != nil { 47 return err 48 } 49 50 return nil 51 } 52 53 func (g *IotGenerator) loadThingTypes(svc *iot.Client) error { 54 output, err := svc.ListThingTypes(context.TODO(), &iot.ListThingTypesInput{}) 55 if err != nil { 56 return err 57 } 58 for _, thingType := range output.ThingTypes { 59 g.Resources = append(g.Resources, terraformutils.NewResource( 60 *thingType.ThingTypeName, 61 *thingType.ThingTypeName, 62 "aws_iot_thing_type", 63 "aws", 64 map[string]string{ 65 "name": *thingType.ThingTypeName, 66 }, 67 iotAllowEmptyValues, 68 map[string]interface{}{}, 69 )) 70 } 71 return nil 72 } 73 74 func (g *IotGenerator) loadThings(svc *iot.Client) error { 75 output, err := svc.ListThings(context.TODO(), &iot.ListThingsInput{}) 76 if err != nil { 77 return err 78 } 79 for _, thing := range output.Things { 80 g.Resources = append(g.Resources, terraformutils.NewResource( 81 *thing.ThingName, 82 *thing.ThingName, 83 "aws_iot_thing", 84 "aws", 85 map[string]string{ 86 "name": *thing.ThingName, 87 }, 88 iotAllowEmptyValues, 89 map[string]interface{}{}, 90 )) 91 } 92 return nil 93 } 94 95 func (g *IotGenerator) loadTopicRules(svc *iot.Client) error { 96 output, err := svc.ListTopicRules(context.TODO(), &iot.ListTopicRulesInput{}) 97 if err != nil { 98 return err 99 } 100 for _, rule := range output.Rules { 101 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 102 *rule.RuleName, 103 *rule.RuleName, 104 "aws_iot_topic_rule", 105 "aws", 106 iotAllowEmptyValues)) 107 } 108 return nil 109 } 110 111 func (g *IotGenerator) loadRoleAliases(svc *iot.Client) error { 112 output, err := svc.ListRoleAliases(context.TODO(), &iot.ListRoleAliasesInput{}) 113 if err != nil { 114 return err 115 } 116 for _, roleAlias := range output.RoleAliases { 117 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 118 roleAlias, 119 roleAlias, 120 "aws_iot_role_alias", 121 "aws", 122 iotAllowEmptyValues)) 123 } 124 return nil 125 }