Skip to content

Commit

Permalink
feat: add timestamp support to http poller (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
obs-gh-alexlew authored Jan 30, 2024
1 parent a77868a commit a800fda
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 3 deletions.
7 changes: 7 additions & 0 deletions client/internal/meta/operation/poller.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ fragment Poller on Poller {
type
}
}
timestamps {
name
source
format
offset
truncate
}
}
... on PollerGCPMonitoringConfig {
projectId
Expand Down
67 changes: 64 additions & 3 deletions client/meta/genqlient.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
16 changes: 16 additions & 0 deletions docs/resources/poller.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Optional:
- `request` (Block List) (see [below for nested schema](#nestedblock--http--request))
- `rule` (Block List) (see [below for nested schema](#nestedblock--http--rule))
- `template` (Block List, Max: 1) (see [below for nested schema](#nestedblock--http--template))
- `timestamp` (Block List) (see [below for nested schema](#nestedblock--http--timestamp))

<a id="nestedblock--http--request"></a>
### Nested Schema for `http.request`
Expand Down Expand Up @@ -185,6 +186,21 @@ Optional:
- `username` (String)


<a id="nestedblock--http--timestamp"></a>
### Nested Schema for `http.timestamp`

Required:

- `name` (String)

Optional:

- `format` (String)
- `offset` (String)
- `source` (String)
- `truncate` (String)



<a id="nestedblock--mongodbatlas"></a>
### Nested Schema for `mongodbatlas`
Expand Down
115 changes: 115 additions & 0 deletions observe/resource_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,37 @@ 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,
ValidateDiagFunc: validateTimeDuration,
},
"truncate": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validateTimeDuration,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -414,6 +445,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 +655,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 +927,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
}
55 changes: 55 additions & 0 deletions observe/resource_poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,61 @@ func TestAccObservePollerHTTP(t *testing.T) {
resource.TestCheckResourceAttrSet("observe_poller.first", "datastream"),
),
},
{
Config: fmt.Sprintf(configPreamble+`
resource "observe_datastream" "example" {
workspace = data.observe_workspace.default.oid
name = "%s-%s"
icon_url = "test"
}
resource "observe_poller" "timestamp" {
workspace = data.observe_workspace.default.oid
name = "%s-%s"
interval = "1m"
retries = 5
datastream = observe_datastream.example.oid
skip_external_validation = true
http {
request {
username = "user"
password = "pass"
auth_scheme = "Digest"
url = "https://example.com/path"
}
timestamp {
name = "now"
format = "RFC822"
truncate = "1s"
}
timestamp {
name = "start"
source = "now"
format = "RFC822"
offset = "1h"
truncate = "1s"
}
}
}`, randomPrefix, "pollers", randomPrefix, "http"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("observe_poller.timestamp", "name", randomPrefix+"-http"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "kind", "HTTP"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "interval", "1m0s"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "http.0.template.#", "0"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "http.0.request.0.url", "https://example.com/path"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "http.0.request.0.auth_scheme", "Digest"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "http.0.timestamp.0.name", "now"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "http.0.timestamp.0.format", "RFC822"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "http.0.timestamp.0.truncate", "1s"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "http.0.timestamp.1.name", "start"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "http.0.timestamp.1.source", "now"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "http.0.timestamp.1.format", "RFC822"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "http.0.timestamp.1.offset", "1h"),
resource.TestCheckResourceAttr("observe_poller.timestamp", "http.0.timestamp.1.truncate", "1s"),
),
},
},
})
}

0 comments on commit a800fda

Please sign in to comment.