|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceSumologicGCPSource() *schema.Resource { |
| 14 | + gcpSource := resourceSumologicSource() |
| 15 | + gcpSource.Create = resourceSumologicGCPSourceCreate |
| 16 | + gcpSource.Read = resourceSumologicGCPSourceRead |
| 17 | + gcpSource.Update = resourceSumologicGCPSourceUpdate |
| 18 | + gcpSource.Importer = &schema.ResourceImporter{ |
| 19 | + State: resourceSumologicSourceImport, |
| 20 | + } |
| 21 | + |
| 22 | + gcpSource.Schema["content_type"] = &schema.Schema{ |
| 23 | + Type: schema.TypeString, |
| 24 | + Optional: true, |
| 25 | + ForceNew: true, |
| 26 | + Default: "GoogleCloudLogs", |
| 27 | + ValidateFunc: validation.StringInSlice([]string{"GoogleCloudLogs"}, false), |
| 28 | + } |
| 29 | + gcpSource.Schema["message_per_request"] = &schema.Schema{ |
| 30 | + Type: schema.TypeBool, |
| 31 | + Optional: true, |
| 32 | + Default: false, |
| 33 | + } |
| 34 | + gcpSource.Schema["url"] = &schema.Schema{ |
| 35 | + Type: schema.TypeString, |
| 36 | + Computed: true, |
| 37 | + } |
| 38 | + gcpSource.Schema["authentication"] = &schema.Schema{ |
| 39 | + Type: schema.TypeList, |
| 40 | + Optional: true, |
| 41 | + ForceNew: true, |
| 42 | + MinItems: 1, |
| 43 | + MaxItems: 1, |
| 44 | + Elem: &schema.Resource{ |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "type": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Optional: true, |
| 49 | + Default: "NoAuthentication", |
| 50 | + ValidateFunc: validation.StringInSlice([]string{"NoAuthentication"}, false), |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + gcpSource.Schema["path"] = &schema.Schema{ |
| 56 | + Type: schema.TypeList, |
| 57 | + Optional: true, |
| 58 | + ForceNew: true, |
| 59 | + MinItems: 1, |
| 60 | + MaxItems: 1, |
| 61 | + Elem: &schema.Resource{ |
| 62 | + Schema: map[string]*schema.Schema{ |
| 63 | + "type": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Optional: true, |
| 66 | + Default: "NoPathExpression", |
| 67 | + ValidateFunc: validation.StringInSlice([]string{"NoPathExpression"}, false), |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + return gcpSource |
| 74 | +} |
| 75 | + |
| 76 | +func resourceSumologicGCPSourceCreate(d *schema.ResourceData, meta interface{}) error { |
| 77 | + c := meta.(*Client) |
| 78 | + |
| 79 | + if d.Id() == "" { |
| 80 | + source, err := resourceToGCPSource(d) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + sourceID, err := c.CreateGCPSource(source, d.Get("collector_id").(int)) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + d.SetId(strconv.Itoa(sourceID)) |
| 91 | + } |
| 92 | + |
| 93 | + return resourceSumologicGCPSourceRead(d, meta) |
| 94 | +} |
| 95 | + |
| 96 | +func resourceSumologicGCPSourceUpdate(d *schema.ResourceData, meta interface{}) error { |
| 97 | + c := meta.(*Client) |
| 98 | + |
| 99 | + source, err := resourceToGCPSource(d) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + err = c.UpdateGCPSource(source, d.Get("collector_id").(int)) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + return resourceSumologicGCPSourceRead(d, meta) |
| 110 | +} |
| 111 | + |
| 112 | +func resourceSumologicGCPSourceRead(d *schema.ResourceData, meta interface{}) error { |
| 113 | + c := meta.(*Client) |
| 114 | + |
| 115 | + id, _ := strconv.Atoi(d.Id()) |
| 116 | + source, err := c.GetGCPSource(d.Get("collector_id").(int), id) |
| 117 | + |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + if source == nil { |
| 123 | + log.Printf("[WARN] GCP source not found, removing from state: %v - %v", id, err) |
| 124 | + d.SetId("") |
| 125 | + |
| 126 | + return nil |
| 127 | + } |
| 128 | + |
| 129 | + GCPResources := source.ThirdPartyRef.Resources |
| 130 | + path := getGCPThirdPartyPathAttributes(GCPResources) |
| 131 | + |
| 132 | + if err := d.Set("path", path); err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + if err := resourceSumologicSourceRead(d, source.Source); err != nil { |
| 137 | + return fmt.Errorf("%s", err) |
| 138 | + } |
| 139 | + d.Set("content_type", source.ContentType) |
| 140 | + d.Set("url", source.URL) |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func resourceToGCPSource(d *schema.ResourceData) (GCPSource, error) { |
| 146 | + source := resourceToSource(d) |
| 147 | + source.Type = "HTTP" |
| 148 | + |
| 149 | + gcpSource := GCPSource{ |
| 150 | + Source: source, |
| 151 | + MessagePerRequest: d.Get("message_per_request").(bool), |
| 152 | + } |
| 153 | + |
| 154 | + authSettings, errAuthSettings := getGCPAuthentication(d) |
| 155 | + if errAuthSettings != nil { |
| 156 | + return gcpSource, errAuthSettings |
| 157 | + } |
| 158 | + |
| 159 | + pathSettings, errPathSettings := getGCPPathSettings(d) |
| 160 | + if errPathSettings != nil { |
| 161 | + return gcpSource, errPathSettings |
| 162 | + } |
| 163 | + |
| 164 | + GCPResource := GCPResource{ |
| 165 | + ServiceType: d.Get("content_type").(string), |
| 166 | + Authentication: authSettings, |
| 167 | + Path: pathSettings, |
| 168 | + } |
| 169 | + |
| 170 | + gcpSource.ThirdPartyRef.Resources = append(gcpSource.ThirdPartyRef.Resources, GCPResource) |
| 171 | + |
| 172 | + return gcpSource, nil |
| 173 | +} |
| 174 | + |
| 175 | +func getGCPThirdPartyPathAttributes(GCPResource []GCPResource) []map[string]interface{} { |
| 176 | + |
| 177 | + var s []map[string]interface{} |
| 178 | + |
| 179 | + for _, t := range GCPResource { |
| 180 | + mapping := map[string]interface{}{ |
| 181 | + "type": t.Path.Type, |
| 182 | + } |
| 183 | + s = append(s, mapping) |
| 184 | + } |
| 185 | + return s |
| 186 | +} |
| 187 | + |
| 188 | +func getGCPAuthentication(d *schema.ResourceData) (GCPAuthentication, error) { |
| 189 | + auths := d.Get("authentication").([]interface{}) |
| 190 | + authSettings := GCPAuthentication{} |
| 191 | + |
| 192 | + if len(auths) > 0 { |
| 193 | + auth := auths[0].(map[string]interface{}) |
| 194 | + switch authType := auth["type"].(string); authType { |
| 195 | + case "NoAuthentication": |
| 196 | + authSettings.Type = "NoAuthentication" |
| 197 | + default: |
| 198 | + errorMessage := fmt.Sprintf("[ERROR] Unknown authType: %v", authType) |
| 199 | + log.Print(errorMessage) |
| 200 | + return authSettings, errors.New(errorMessage) |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + return authSettings, nil |
| 205 | +} |
| 206 | + |
| 207 | +func getGCPPathSettings(d *schema.ResourceData) (GCPPath, error) { |
| 208 | + pathSettings := GCPPath{} |
| 209 | + paths := d.Get("path").([]interface{}) |
| 210 | + |
| 211 | + if len(paths) > 0 { |
| 212 | + path := paths[0].(map[string]interface{}) |
| 213 | + switch pathType := path["type"].(string); pathType { |
| 214 | + case "NoPathExpression": |
| 215 | + pathSettings.Type = "NoPathExpression" |
| 216 | + default: |
| 217 | + errorMessage := fmt.Sprintf("[ERROR] Unknown resourceType in path: %v", pathType) |
| 218 | + log.Print(errorMessage) |
| 219 | + return pathSettings, errors.New(errorMessage) |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return pathSettings, nil |
| 224 | +} |
0 commit comments