|
| 1 | +package myrasec |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "strconv" |
| 7 | + "sync" |
| 8 | + |
| 9 | + "github.com/GoogleCloudPlatform/terraformer/terraformutils" |
| 10 | + mgo "github.com/Myra-Security-GmbH/myrasec-go/v2" |
| 11 | +) |
| 12 | + |
| 13 | +// TagSettingGenerator |
| 14 | +type TagSettingGenerator struct { |
| 15 | + MyrasecService |
| 16 | +} |
| 17 | + |
| 18 | +// createTagSettingResources |
| 19 | +func (g *TagSettingGenerator) createTagSettingResources(api *mgo.API, tag mgo.Tag, wg *sync.WaitGroup) error { |
| 20 | + defer wg.Done() |
| 21 | + |
| 22 | + response, err := api.ListTagSettingsMap(tag.ID) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + |
| 27 | + r := terraformutils.NewResource( |
| 28 | + strconv.Itoa(tag.ID), |
| 29 | + fmt.Sprintf("%s_%d", tag.Name, tag.ID), |
| 30 | + "myrasec_tag_settings", |
| 31 | + "myrasec", |
| 32 | + map[string]string{ |
| 33 | + "tag_id": strconv.Itoa(tag.ID), |
| 34 | + }, |
| 35 | + []string{}, |
| 36 | + map[string]any{}, |
| 37 | + ) |
| 38 | + |
| 39 | + attributes := structToMap(mgo.Settings{}) |
| 40 | + data := *(response.(*map[string]any)) |
| 41 | + settings := data["settings"].(map[string]any) |
| 42 | + for k := range attributes { |
| 43 | + if _, ok := settings[k]; !ok { |
| 44 | + r.IgnoreKeys = append(r.IgnoreKeys, k) |
| 45 | + } |
| 46 | + } |
| 47 | + g.Resources = append(g.Resources, r) |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +// InitResources |
| 52 | +func (g *TagSettingGenerator) InitResources() error { |
| 53 | + wg := sync.WaitGroup{} |
| 54 | + |
| 55 | + api, err := g.initializeAPI() |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + funcs := []func(*mgo.API, mgo.Tag, *sync.WaitGroup) error{ |
| 61 | + g.createTagSettingResources, |
| 62 | + } |
| 63 | + err = createResourcesPerTag(api, funcs, &wg, "CONFIG") |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + wg.Wait() |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func index(s, substr string) int { |
| 74 | + for i := range s { |
| 75 | + if s[i:i+len(substr)] == substr { |
| 76 | + return i |
| 77 | + } |
| 78 | + } |
| 79 | + return -1 |
| 80 | +} |
| 81 | + |
| 82 | +func structToMap(s any) map[string]any { |
| 83 | + result := make(map[string]any) |
| 84 | + val := reflect.ValueOf(s) |
| 85 | + typ := reflect.TypeOf(s) |
| 86 | + |
| 87 | + // Make sure it's a struct |
| 88 | + if val.Kind() == reflect.Ptr { |
| 89 | + val = val.Elem() |
| 90 | + typ = typ.Elem() |
| 91 | + } |
| 92 | + if val.Kind() != reflect.Struct { |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + for i := 0; i < val.NumField(); i++ { |
| 97 | + field := typ.Field(i) |
| 98 | + value := val.Field(i) |
| 99 | + |
| 100 | + // get JSON tag |
| 101 | + jsonTag := field.Tag.Get("json") |
| 102 | + |
| 103 | + // if no json tag or "-", skip or fallback to field name |
| 104 | + if jsonTag == "" || jsonTag == "-" { |
| 105 | + jsonTag = field.Name |
| 106 | + } else { |
| 107 | + if commaIdx := reflect.ValueOf(jsonTag).String(); commaIdx != "" { |
| 108 | + if idx := index(jsonTag, ","); idx != -1 { |
| 109 | + jsonTag = jsonTag[:idx] |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + result[jsonTag] = value.Interface() |
| 114 | + } |
| 115 | + return result |
| 116 | +} |
0 commit comments