Skip to content

Commit

Permalink
fix: add helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
obs-gh-alexlew committed Jan 24, 2024
1 parent 699f447 commit 12ecb43
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
18 changes: 18 additions & 0 deletions client/meta/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ var AllPollerHTTPRequestAuthSchemes = []PollerHTTPRequestAuthScheme{
PollerHTTPRequestAuthSchemeDigest,
}

var AllPollerHTTPTimestampFormats = []PollerHTTPTimestampFormatScheme{
PollerHTTPTimestampFormatSchemeAnsic,
PollerHTTPTimestampFormatSchemeUnixdate,
PollerHTTPTimestampFormatSchemeRubydate,
PollerHTTPTimestampFormatSchemeRfc822,
PollerHTTPTimestampFormatSchemeRfc822z,
PollerHTTPTimestampFormatSchemeRfc850,
PollerHTTPTimestampFormatSchemeRfc1123,
PollerHTTPTimestampFormatSchemeRfc1123z,
PollerHTTPTimestampFormatSchemeRfc3339,
PollerHTTPTimestampFormatSchemeRfc3339nano,
PollerHTTPTimestampFormatSchemeKitchen,
PollerHTTPTimestampFormatSchemeUnix,
PollerHTTPTimestampFormatSchemeUnixmilli,
PollerHTTPTimestampFormatSchemeUnixmicro,
PollerHTTPTimestampFormatSchemeUnixmano,
}

// AllBookmarkKindTypes This list is incomplete and will be filled in
// as we support more types of bookmarks in the terraform provider
var AllBookmarkKindTypes = []BookmarkKind{
Expand Down
113 changes: 113 additions & 0 deletions observe/resource_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,35 @@ func resourcePoller() *schema.Resource {
},
},
},
"timestamp": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"source": {
Type: schema.TypeString,
Optional: true,
},
"format": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateEnums(gql.AllPollerHTTPTimestampFormats),
},
"offset": {
Type: schema.TypeString,
Optional: true,
},
"truncate": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -414,6 +443,7 @@ func newPollerConfig(data *schema.ResourceData) (input *gql.PollerInput, diags d
Headers: &parsedHeaders,
Requests: expandPollerHTTPRequests(data, "http.0.request"),
Rules: expandPollerHTTPRules(data, "http.0.rule"),
Timestamps: expandPollerHTTPTimestamps(data, "http.0.timestamp"),
}

if v, ok := data.GetOk("http.0.endpoint"); ok {
Expand Down Expand Up @@ -623,6 +653,12 @@ func resourcePollerRead(ctx context.Context, data *schema.ResourceData, meta int
ht["rule"] = rule
}

timestamp, timestampDiags := flattenPollerHTTPTimestamps(httpConfig.Timestamps)
diags = append(diags, timestampDiags...)
if !requestDiags.HasError() {
ht["timestamp"] = timestamp
}

if err := data.Set("http", []interface{}{ht}); err != nil {
diags = append(diags, diag.FromErr(err)...)
}
Expand Down Expand Up @@ -889,3 +925,80 @@ func expandPollerHTTPDecoder(data *schema.ResourceData, key string) *gql.PollerH
}
return decoder
}

func flattenPollerHTTPTimestamps(timestamps []gql.PollerConfigPollerHTTPConfigTimestampsPollerHTTPTimestampConfig) (flats []map[string]interface{}, diags diag.Diagnostics) {
if len(timestamps) == 0 {
return
}

for _, t := range timestamps {
timestamp, diag := flattenPollerHTTPTimestamp(&t)
diags = append(diags, diag...)
if !diag.HasError() {
flats = append(flats, timestamp)
}
}

return
}

func flattenPollerHTTPTimestamp(timestamp *gql.PollerConfigPollerHTTPConfigTimestampsPollerHTTPTimestampConfig) (flat map[string]interface{}, diags diag.Diagnostics) {
if timestamp == nil {
return
}

flat = map[string]interface{}{
"name": timestamp.Name,
"source": timestamp.Source,
"format": timestamp.Format,
"offset": timestamp.Offset,
"truncate": timestamp.Truncate,
}

return
}

func expandPollerHTTPTimestamps(data *schema.ResourceData, key string) (timestamps []gql.PollerHTTPTimestampInput) {
l := data.Get(key).([]interface{})
if len(l) == 0 {
return nil
}

for i := range l {
if req := expandPollerHTTPTimestamp(data, fmt.Sprintf("%s.%d", key, i)); req != nil {
timestamps = append(timestamps, *req)
}
}
return
}

func expandPollerHTTPTimestamp(data *schema.ResourceData, key string) *gql.PollerHTTPTimestampInput {
if _, ok := data.GetOk(key); !ok {
return nil
}

timestamp := &gql.PollerHTTPTimestampInput{}

if v, ok := data.GetOk(key + ".name"); ok {
s := v.(string)
timestamp.Name = &s
}
if v, ok := data.GetOk(key + ".source"); ok {
s := v.(string)
timestamp.Source = &s
}
if v, ok := data.GetOk(key + ".format"); ok {
s := gql.PollerHTTPTimestampFormatScheme(v.(string))
timestamp.Format = &s
}
if v, ok := data.GetOk(key + ".offset"); ok {
s := v.(string)
timestamp.Offset = &s
}
if v, ok := data.GetOk(key + ".truncate"); ok {
s := v.(string)
timestamp.Truncate = &s
}

return timestamp
}

0 comments on commit 12ecb43

Please sign in to comment.