Skip to content

Commit

Permalink
Implement unstable SLO resource (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
aschepis authored Dec 2, 2024
1 parent 92092a2 commit 39b9da4
Show file tree
Hide file tree
Showing 51 changed files with 6,815 additions and 26 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

Added:
* Add unstable `chronosphere_slo` resource.

Fixed:
* Fix `chronosphere_resource_pools_config` error when default pool is not set.

Expand Down
80 changes: 80 additions & 0 deletions chronosphere/generated_resources.gen.go

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

23 changes: 18 additions & 5 deletions chronosphere/generateresources/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ type entityType struct {
SwaggerType string
// SwaggerModel represents the underlying model to use. Will normally be the
// same as SwaggerType
SwaggerModel string
SwaggerModel string
// SwaggerParam is the name of the parameter to use in the swagger client request/response
// types. This will normally be the same as SwaggerType, but will be different for the
// SLO resource, where it is "Slo" instead of "SLO".
SwaggerParam string
FieldName string
SwaggerClient string
SwaggerClientPackage string
Expand All @@ -101,6 +105,7 @@ func newEntityType(a api, r registry.Resource) entityType {
GoType: fmt.Sprintf("%s%s", a.GoPrefix, r.Entity),
SwaggerType: r.Entity,
SwaggerModel: r.Entity,
SwaggerParam: r.Entity,
SwaggerClient: fmt.Sprintf("%s.%s", a.Client, r.Entity),
SwaggerClientPackage: strcase.ToSnake(r.Entity),
DisableDryRun: r.DisableDryRun,
Expand All @@ -113,6 +118,14 @@ func newEntityType(a api, r registry.Resource) entityType {
if r.Name == "classic_dashboard" {
et.GoType = fmt.Sprintf("%s%s", a.GoPrefix, "ClassicDashboard")
}

// SLOs are named with all caps so we get some funny business with the swagger generated code
// that we have to account for in our generated resources code.
if r.Name == "slo" {
et.SwaggerClientPackage = "s_l_o"
et.SwaggerParam = "Slo"
et.SwaggerClient = fmt.Sprintf("%s.%s", a.Client, et.SwaggerParam)
}
return et
}

Expand Down Expand Up @@ -174,15 +187,15 @@ func ({{.GoType}}) create(
req := &{{.SwaggerClientPackage}}.Create{{.SwaggerType}}Params{
Context: ctx,
Body: &{{.API.Package}}models.{{.API.SwaggerPrefix}}Create{{.SwaggerType}}Request{
{{.SwaggerType}}: m,
{{.SwaggerParam}}: m,
{{ if not .DisableDryRun }} DryRun: dryRun, {{ end }}
},
}
resp, err := clients.{{.SwaggerClient}}.Create{{.SwaggerType}}(req)
if err != nil {
return "", err
}
e := resp.Payload.{{.SwaggerType}}
e := resp.Payload.{{.SwaggerParam}}
if e == nil {
return "", nil
}
Expand All @@ -204,7 +217,7 @@ func ({{.GoType}}) read(
if err != nil {
return nil, err
}
return resp.Payload.{{.SwaggerType}}, nil
return resp.Payload.{{.SwaggerParam}}, nil
}
{{ if not .UpdateUnsupported -}}
Expand All @@ -229,7 +242,7 @@ func ({{.GoType}}) update(
{{ else }}
Body: &{{.API.Package}}models.{{.API.SwaggerPrefix}}Update{{.SwaggerType}}Request{
{{ end }}
{{.SwaggerType}}: m,
{{.SwaggerParam}}: m,
CreateIfMissing: params.createIfMissing,
{{ if not .DisableDryRun }} DryRun: params.dryRun, {{ end }}
},
Expand Down
1 change: 1 addition & 0 deletions chronosphere/intschema/generateintschema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var sharedSchemaTypeNames = map[*schema.Schema]string{
tfschema.TraceStringFilterSchema: "TraceStringFilter",
tfschema.TraceTagFilterSchema: "TraceTagFilter",
tfschema.ValueMappingsSchema: "ValueMappings",
tfschema.SLOAdditionalPromQLFilters: "SLOAdditionalPromQLFilters",
}

// Add shared element references here to generate shared types. Usually we
Expand Down
6 changes: 6 additions & 0 deletions chronosphere/intschema/shared_schemas.go

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

100 changes: 100 additions & 0 deletions chronosphere/intschema/slo.go

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

47 changes: 30 additions & 17 deletions chronosphere/pagination/generatepagination/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,37 @@ type api struct {
}

type entityType struct {
API api
Singular string
Plural string
SwaggerClient string
SwaggerClientPackage string
SwaggerModel string
Disable bool
API api
Singular string
Plural string
SwaggerAPIMethodPlural string
SwaggerClient string
SwaggerClientPackage string
SwaggerModel string
Disable bool
}

func newEntityType(a api, e registry.Resource) entityType {
return entityType{
API: a,
Singular: e.Entity,
Plural: plural(e.Entity),
SwaggerClientPackage: strcase.ToSnake(e.Entity),
Disable: e.DisableExportImport,
SwaggerModel: e.Entity,
et := entityType{
API: a,
Singular: e.Entity,
Plural: plural(e.Entity),
SwaggerAPIMethodPlural: plural(e.Entity),
SwaggerClientPackage: strcase.ToSnake(e.Entity),
Disable: e.DisableExportImport,
SwaggerModel: e.Entity,
}

// SLOs are named with all caps so we get some funny business with the swagger generated code
// that we have to account for in our generated resources code.
if e.Entity == "SLO" {
et.SwaggerClientPackage = "s_l_o"
et.Singular = "Slo"
et.Plural = "Slos"
et.SwaggerAPIMethodPlural = "SLOs"
}

return et
}

func newClassicDashboard(a api) entityType {
Expand Down Expand Up @@ -175,7 +188,7 @@ func List{{if .API.RequireUnstable}}Unstable{{end}}{{.Plural}}ByFilter(
ctx context.Context,
client *{{.API.Package}}.Client,
f Filter,
opts... func(*{{.SwaggerClientPackage}}.List{{.Plural}}Params),
opts... func(*{{.SwaggerClientPackage}}.List{{.SwaggerAPIMethodPlural}}Params),
) ([]*{{.API.Package}}models.{{.API.SwaggerPrefix}}{{.SwaggerModel}}, error) {
{{- if .Disable}}
return nil, nil
Expand All @@ -190,7 +203,7 @@ func List{{if .API.RequireUnstable}}Unstable{{end}}{{.Plural}}ByFilter(
result []*{{.API.Package}}models.{{.API.SwaggerPrefix}}{{.SwaggerModel}}
)
for {
p := &{{.SwaggerClientPackage}}.List{{.Plural}}Params{
p := &{{.SwaggerClientPackage}}.List{{.SwaggerAPIMethodPlural}}Params{
Context: ctx,
PageToken: &nextToken,
Slugs: f.Slugs,
Expand All @@ -199,7 +212,7 @@ func List{{if .API.RequireUnstable}}Unstable{{end}}{{.Plural}}ByFilter(
for _, opt := range opts {
opt(p)
}
resp, err := client.{{.Singular}}.List{{.Plural}}(p)
resp, err := client.{{.Singular}}.List{{.SwaggerAPIMethodPlural}}(p)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 39b9da4

Please sign in to comment.