Fix Argus scrape config internal models to use Terraform types (#349)
* Fix SAML2 model * Fix BasicAuth model * Fix Targets model * Fixes after testing
This commit is contained in:
parent
8ddcfc8e43
commit
e48deaff59
3 changed files with 481 additions and 118 deletions
|
|
@ -222,7 +222,7 @@ func (d *scrapeConfigDataSource) Read(ctx context.Context, req datasource.ReadRe
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = mapFields(scResp.Data, &model)
|
err = mapFields(ctx, scResp.Data, &model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Mapping fields", err.Error())
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Mapping fields", err.Error())
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||||
|
|
@ -59,25 +60,45 @@ type Model struct {
|
||||||
ScrapeInterval types.String `tfsdk:"scrape_interval"`
|
ScrapeInterval types.String `tfsdk:"scrape_interval"`
|
||||||
ScrapeTimeout types.String `tfsdk:"scrape_timeout"`
|
ScrapeTimeout types.String `tfsdk:"scrape_timeout"`
|
||||||
SampleLimit types.Int64 `tfsdk:"sample_limit"`
|
SampleLimit types.Int64 `tfsdk:"sample_limit"`
|
||||||
SAML2 *SAML2 `tfsdk:"saml2"`
|
SAML2 types.Object `tfsdk:"saml2"`
|
||||||
BasicAuth *BasicAuth `tfsdk:"basic_auth"`
|
BasicAuth types.Object `tfsdk:"basic_auth"`
|
||||||
Targets []Target `tfsdk:"targets"`
|
Targets types.List `tfsdk:"targets"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SAML2 struct {
|
// Struct corresponding to Model.SAML2
|
||||||
|
type saml2Model struct {
|
||||||
EnableURLParameters types.Bool `tfsdk:"enable_url_parameters"`
|
EnableURLParameters types.Bool `tfsdk:"enable_url_parameters"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Target struct {
|
// Types corresponding to saml2Model
|
||||||
URLs []types.String `tfsdk:"urls"`
|
var saml2Types = map[string]attr.Type{
|
||||||
Labels types.Map `tfsdk:"labels"`
|
"enable_url_parameters": types.BoolType,
|
||||||
}
|
}
|
||||||
|
|
||||||
type BasicAuth struct {
|
// Struct corresponding to Model.BasicAuth
|
||||||
|
type basicAuthModel struct {
|
||||||
Username types.String `tfsdk:"username"`
|
Username types.String `tfsdk:"username"`
|
||||||
Password types.String `tfsdk:"password"`
|
Password types.String `tfsdk:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Types corresponding to basicAuthModel
|
||||||
|
var basicAuthTypes = map[string]attr.Type{
|
||||||
|
"username": types.StringType,
|
||||||
|
"password": types.StringType,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Struct corresponding to Model.Targets[i]
|
||||||
|
type targetModel struct {
|
||||||
|
URLs types.List `tfsdk:"urls"`
|
||||||
|
Labels types.Map `tfsdk:"labels"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Types corresponding to targetModel
|
||||||
|
var targetTypes = map[string]attr.Type{
|
||||||
|
"urls": types.ListType{ElemType: types.StringType},
|
||||||
|
"labels": types.MapType{ElemType: types.StringType},
|
||||||
|
}
|
||||||
|
|
||||||
// NewScrapeConfigResource is a helper function to simplify the provider implementation.
|
// NewScrapeConfigResource is a helper function to simplify the provider implementation.
|
||||||
func NewScrapeConfigResource() resource.Resource {
|
func NewScrapeConfigResource() resource.Resource {
|
||||||
return &scrapeConfigResource{}
|
return &scrapeConfigResource{}
|
||||||
|
|
@ -240,6 +261,7 @@ func (r *scrapeConfigResource) Schema(_ context.Context, _ resource.SchemaReques
|
||||||
"basic_auth": schema.SingleNestedAttribute{
|
"basic_auth": schema.SingleNestedAttribute{
|
||||||
Description: "A basic authentication block.",
|
Description: "A basic authentication block.",
|
||||||
Optional: true,
|
Optional: true,
|
||||||
|
Computed: true,
|
||||||
Attributes: map[string]schema.Attribute{
|
Attributes: map[string]schema.Attribute{
|
||||||
"username": schema.StringAttribute{
|
"username": schema.StringAttribute{
|
||||||
Description: "Specifies basic auth username.",
|
Description: "Specifies basic auth username.",
|
||||||
|
|
@ -304,8 +326,35 @@ func (r *scrapeConfigResource) Create(ctx context.Context, req resource.CreateRe
|
||||||
instanceId := model.InstanceId.ValueString()
|
instanceId := model.InstanceId.ValueString()
|
||||||
scName := model.Name.ValueString()
|
scName := model.Name.ValueString()
|
||||||
|
|
||||||
|
saml2Model := saml2Model{}
|
||||||
|
if !model.SAML2.IsNull() && !model.SAML2.IsUnknown() {
|
||||||
|
diags = model.SAML2.As(ctx, &saml2Model, basetypes.ObjectAsOptions{})
|
||||||
|
resp.Diagnostics.Append(diags...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
basicAuthModel := basicAuthModel{}
|
||||||
|
if !model.BasicAuth.IsNull() && !model.BasicAuth.IsUnknown() {
|
||||||
|
diags = model.BasicAuth.As(ctx, &basicAuthModel, basetypes.ObjectAsOptions{})
|
||||||
|
resp.Diagnostics.Append(diags...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
targetsModel := []targetModel{}
|
||||||
|
if !model.Targets.IsNull() && !model.Targets.IsUnknown() {
|
||||||
|
diags = model.Targets.ElementsAs(ctx, &targetsModel, false)
|
||||||
|
resp.Diagnostics.Append(diags...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Generate API request body from model
|
// Generate API request body from model
|
||||||
payload, err := toCreatePayload(ctx, &model)
|
payload, err := toCreatePayload(ctx, &model, &saml2Model, &basicAuthModel, targetsModel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating scrape config", fmt.Sprintf("Creating API payload: %v", err))
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating scrape config", fmt.Sprintf("Creating API payload: %v", err))
|
||||||
return
|
return
|
||||||
|
|
@ -325,7 +374,7 @@ func (r *scrapeConfigResource) Create(ctx context.Context, req resource.CreateRe
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating scrape config", fmt.Sprintf("Calling API for updated data: %v", err))
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating scrape config", fmt.Sprintf("Calling API for updated data: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = mapFields(got.Data, &model)
|
err = mapFields(ctx, got.Data, &model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating scrape config", fmt.Sprintf("Processing API payload: %v", err))
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating scrape config", fmt.Sprintf("Processing API payload: %v", err))
|
||||||
return
|
return
|
||||||
|
|
@ -363,7 +412,7 @@ func (r *scrapeConfigResource) Read(ctx context.Context, req resource.ReadReques
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map response body to schema
|
// Map response body to schema
|
||||||
err = mapFields(scResp.Data, &model)
|
err = mapFields(ctx, scResp.Data, &model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading scrape config", fmt.Sprintf("Processing API payload: %v", err))
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading scrape config", fmt.Sprintf("Processing API payload: %v", err))
|
||||||
return
|
return
|
||||||
|
|
@ -390,8 +439,35 @@ func (r *scrapeConfigResource) Update(ctx context.Context, req resource.UpdateRe
|
||||||
instanceId := model.InstanceId.ValueString()
|
instanceId := model.InstanceId.ValueString()
|
||||||
scName := model.Name.ValueString()
|
scName := model.Name.ValueString()
|
||||||
|
|
||||||
|
saml2Model := saml2Model{}
|
||||||
|
if !model.SAML2.IsNull() && !model.SAML2.IsUnknown() {
|
||||||
|
diags = model.SAML2.As(ctx, &saml2Model, basetypes.ObjectAsOptions{})
|
||||||
|
resp.Diagnostics.Append(diags...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
basicAuthModel := basicAuthModel{}
|
||||||
|
if !model.BasicAuth.IsNull() && !model.BasicAuth.IsUnknown() {
|
||||||
|
diags = model.BasicAuth.As(ctx, &basicAuthModel, basetypes.ObjectAsOptions{})
|
||||||
|
resp.Diagnostics.Append(diags...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
targetsModel := []targetModel{}
|
||||||
|
if !model.Targets.IsNull() && !model.Targets.IsUnknown() {
|
||||||
|
diags = model.Targets.ElementsAs(ctx, &targetsModel, false)
|
||||||
|
resp.Diagnostics.Append(diags...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Generate API request body from model
|
// Generate API request body from model
|
||||||
payload, err := toUpdatePayload(ctx, &model)
|
payload, err := toUpdatePayload(ctx, &model, &saml2Model, &basicAuthModel, targetsModel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating scrape config", fmt.Sprintf("Creating API payload: %v", err))
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating scrape config", fmt.Sprintf("Creating API payload: %v", err))
|
||||||
return
|
return
|
||||||
|
|
@ -410,7 +486,7 @@ func (r *scrapeConfigResource) Update(ctx context.Context, req resource.UpdateRe
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating scrape config", fmt.Sprintf("Calling API for updated data: %v", err))
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating scrape config", fmt.Sprintf("Calling API for updated data: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = mapFields(scResp.Data, &model)
|
err = mapFields(ctx, scResp.Data, &model)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating scrape config", fmt.Sprintf("Processing API payload: %v", err))
|
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating scrape config", fmt.Sprintf("Processing API payload: %v", err))
|
||||||
return
|
return
|
||||||
|
|
@ -471,7 +547,7 @@ func (r *scrapeConfigResource) ImportState(ctx context.Context, req resource.Imp
|
||||||
tflog.Info(ctx, "Argus scrape config state imported")
|
tflog.Info(ctx, "Argus scrape config state imported")
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapFields(sc *argus.Job, model *Model) error {
|
func mapFields(ctx context.Context, sc *argus.Job, model *Model) error {
|
||||||
if sc == nil {
|
if sc == nil {
|
||||||
return fmt.Errorf("response input is nil")
|
return fmt.Errorf("response input is nil")
|
||||||
}
|
}
|
||||||
|
|
@ -503,35 +579,50 @@ func mapFields(sc *argus.Job, model *Model) error {
|
||||||
model.ScrapeInterval = types.StringPointerValue(sc.ScrapeInterval)
|
model.ScrapeInterval = types.StringPointerValue(sc.ScrapeInterval)
|
||||||
model.ScrapeTimeout = types.StringPointerValue(sc.ScrapeTimeout)
|
model.ScrapeTimeout = types.StringPointerValue(sc.ScrapeTimeout)
|
||||||
model.SampleLimit = types.Int64PointerValue(sc.SampleLimit)
|
model.SampleLimit = types.Int64PointerValue(sc.SampleLimit)
|
||||||
handleSAML2(sc, model)
|
err := mapSAML2(sc, model)
|
||||||
handleBasicAuth(sc, model)
|
if err != nil {
|
||||||
handleTargets(sc, model)
|
return fmt.Errorf("map saml2: %w", err)
|
||||||
|
}
|
||||||
|
err = mapBasicAuth(sc, model)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("map basic auth: %w", err)
|
||||||
|
}
|
||||||
|
err = mapTargets(ctx, sc, model)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("map targets: %w", err)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleBasicAuth(sc *argus.Job, model *Model) {
|
func mapBasicAuth(sc *argus.Job, model *Model) error {
|
||||||
if sc.BasicAuth == nil {
|
if sc.BasicAuth == nil {
|
||||||
model.BasicAuth = nil
|
model.BasicAuth = types.ObjectNull(basicAuthTypes)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
model.BasicAuth = &BasicAuth{
|
basicAuthMap := map[string]attr.Value{
|
||||||
Username: types.StringPointerValue(sc.BasicAuth.Username),
|
"username": types.StringValue(*sc.BasicAuth.Username),
|
||||||
Password: types.StringPointerValue(sc.BasicAuth.Password),
|
"password": types.StringValue(*sc.BasicAuth.Password),
|
||||||
}
|
}
|
||||||
|
basicAuthTF, diags := types.ObjectValue(basicAuthTypes, basicAuthMap)
|
||||||
|
if diags.HasError() {
|
||||||
|
return core.DiagsToError(diags)
|
||||||
|
}
|
||||||
|
model.BasicAuth = basicAuthTF
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSAML2(sc *argus.Job, model *Model) {
|
func mapSAML2(sc *argus.Job, model *Model) error {
|
||||||
if (sc.Params == nil || *sc.Params == nil) && model.SAML2 == nil {
|
if (sc.Params == nil || *sc.Params == nil) && model.SAML2.IsNull() {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if model.SAML2 == nil {
|
if model.SAML2.IsNull() || model.SAML2.IsUnknown() {
|
||||||
model.SAML2 = &SAML2{}
|
model.SAML2 = types.ObjectNull(saml2Types)
|
||||||
}
|
}
|
||||||
|
|
||||||
flag := true
|
flag := true
|
||||||
if sc.Params == nil || *sc.Params == nil {
|
if sc.Params == nil || *sc.Params == nil {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
p := *sc.Params
|
p := *sc.Params
|
||||||
if v, ok := p["saml2"]; ok {
|
if v, ok := p["saml2"]; ok {
|
||||||
|
|
@ -540,28 +631,46 @@ func handleSAML2(sc *argus.Job, model *Model) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
model.SAML2 = &SAML2{
|
saml2Map := map[string]attr.Value{
|
||||||
EnableURLParameters: types.BoolValue(flag),
|
"enable_url_parameters": types.BoolValue(flag),
|
||||||
}
|
}
|
||||||
|
saml2TF, diags := types.ObjectValue(saml2Types, saml2Map)
|
||||||
|
if diags.HasError() {
|
||||||
|
return core.DiagsToError(diags)
|
||||||
|
}
|
||||||
|
model.SAML2 = saml2TF
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleTargets(sc *argus.Job, model *Model) {
|
func mapTargets(ctx context.Context, sc *argus.Job, model *Model) error {
|
||||||
if sc == nil || sc.StaticConfigs == nil {
|
if sc == nil || sc.StaticConfigs == nil {
|
||||||
model.Targets = []Target{}
|
model.Targets = types.ListNull(types.ObjectType{AttrTypes: targetTypes})
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
newTargets := []Target{}
|
|
||||||
for i, sc := range *sc.StaticConfigs {
|
targetsModel := []targetModel{}
|
||||||
nt := Target{
|
if !model.Targets.IsNull() && !model.Targets.IsUnknown() {
|
||||||
URLs: []types.String{},
|
diags := model.Targets.ElementsAs(ctx, &targetsModel, false)
|
||||||
|
if diags.HasError() {
|
||||||
|
return core.DiagsToError(diags)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
newTargets := []attr.Value{}
|
||||||
|
for i, sc := range *sc.StaticConfigs {
|
||||||
|
nt := targetModel{}
|
||||||
|
|
||||||
|
// Map URLs
|
||||||
|
urls := []attr.Value{}
|
||||||
if sc.Targets != nil {
|
if sc.Targets != nil {
|
||||||
for _, v := range *sc.Targets {
|
for _, v := range *sc.Targets {
|
||||||
nt.URLs = append(nt.URLs, types.StringValue(v))
|
urls = append(urls, types.StringValue(v))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
nt.URLs = types.ListValueMust(types.StringType, urls)
|
||||||
|
|
||||||
if len(model.Targets) > i && model.Targets[i].Labels.IsNull() || sc.Labels == nil {
|
// Map Labels
|
||||||
|
if len(model.Targets.Elements()) > i && targetsModel[i].Labels.IsNull() || sc.Labels == nil {
|
||||||
nt.Labels = types.MapNull(types.StringType)
|
nt.Labels = types.MapNull(types.StringType)
|
||||||
} else {
|
} else {
|
||||||
newl := map[string]attr.Value{}
|
newl := map[string]attr.Value{}
|
||||||
|
|
@ -570,12 +679,30 @@ func handleTargets(sc *argus.Job, model *Model) {
|
||||||
}
|
}
|
||||||
nt.Labels = types.MapValueMust(types.StringType, newl)
|
nt.Labels = types.MapValueMust(types.StringType, newl)
|
||||||
}
|
}
|
||||||
newTargets = append(newTargets, nt)
|
|
||||||
|
// Build target
|
||||||
|
targetMap := map[string]attr.Value{
|
||||||
|
"urls": nt.URLs,
|
||||||
|
"labels": nt.Labels,
|
||||||
|
}
|
||||||
|
targetTF, diags := types.ObjectValue(targetTypes, targetMap)
|
||||||
|
if diags.HasError() {
|
||||||
|
return core.DiagsToError(diags)
|
||||||
|
}
|
||||||
|
|
||||||
|
newTargets = append(newTargets, targetTF)
|
||||||
}
|
}
|
||||||
model.Targets = newTargets
|
|
||||||
|
targetsTF, diags := types.ListValue(types.ObjectType{AttrTypes: targetTypes}, newTargets)
|
||||||
|
if diags.HasError() {
|
||||||
|
return core.DiagsToError(diags)
|
||||||
|
}
|
||||||
|
|
||||||
|
model.Targets = targetsTF
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func toCreatePayload(ctx context.Context, model *Model) (*argus.CreateScrapeConfigPayload, error) {
|
func toCreatePayload(ctx context.Context, model *Model, saml2Model *saml2Model, basicAuthModel *basicAuthModel, targetsModel []targetModel) (*argus.CreateScrapeConfigPayload, error) {
|
||||||
if model == nil {
|
if model == nil {
|
||||||
return nil, fmt.Errorf("nil model")
|
return nil, fmt.Errorf("nil model")
|
||||||
}
|
}
|
||||||
|
|
@ -589,47 +716,52 @@ func toCreatePayload(ctx context.Context, model *Model) (*argus.CreateScrapeConf
|
||||||
SampleLimit: utils.Ptr(float64(model.SampleLimit.ValueInt64())),
|
SampleLimit: utils.Ptr(float64(model.SampleLimit.ValueInt64())),
|
||||||
Scheme: conversion.StringValueToPointer(model.Scheme),
|
Scheme: conversion.StringValueToPointer(model.Scheme),
|
||||||
}
|
}
|
||||||
setDefaultsCreateScrapeConfig(&sc, model)
|
setDefaultsCreateScrapeConfig(&sc, model, saml2Model)
|
||||||
|
|
||||||
if model.SAML2 != nil && !model.SAML2.EnableURLParameters.ValueBool() {
|
if !saml2Model.EnableURLParameters.IsNull() && !saml2Model.EnableURLParameters.IsUnknown() {
|
||||||
m := make(map[string]interface{})
|
m := make(map[string]interface{})
|
||||||
if sc.Params != nil {
|
if sc.Params != nil {
|
||||||
m = *sc.Params
|
m = *sc.Params
|
||||||
}
|
}
|
||||||
m["saml2"] = []string{"disabled"}
|
if saml2Model.EnableURLParameters.ValueBool() {
|
||||||
|
m["saml2"] = []string{"enabled"}
|
||||||
|
} else {
|
||||||
|
m["saml2"] = []string{"disabled"}
|
||||||
|
}
|
||||||
sc.Params = &m
|
sc.Params = &m
|
||||||
}
|
}
|
||||||
|
|
||||||
if model.BasicAuth != nil {
|
if sc.BasicAuth == nil && !basicAuthModel.Username.IsNull() && !basicAuthModel.Password.IsNull() {
|
||||||
if sc.BasicAuth == nil {
|
sc.BasicAuth = &argus.CreateScrapeConfigPayloadBasicAuth{
|
||||||
sc.BasicAuth = &argus.CreateScrapeConfigPayloadBasicAuth{
|
Username: conversion.StringValueToPointer(basicAuthModel.Username),
|
||||||
Username: conversion.StringValueToPointer(model.BasicAuth.Username),
|
Password: conversion.StringValueToPointer(basicAuthModel.Password),
|
||||||
Password: conversion.StringValueToPointer(model.BasicAuth.Password),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t := make([]argus.CreateScrapeConfigPayloadStaticConfigsInner, len(model.Targets))
|
t := make([]argus.CreateScrapeConfigPayloadStaticConfigsInner, len(targetsModel))
|
||||||
for i, target := range model.Targets {
|
for i, target := range targetsModel {
|
||||||
ti := argus.CreateScrapeConfigPayloadStaticConfigsInner{}
|
ti := argus.CreateScrapeConfigPayloadStaticConfigsInner{}
|
||||||
tgts := []string{}
|
|
||||||
for _, v := range target.URLs {
|
|
||||||
tgts = append(tgts, v.ValueString())
|
|
||||||
}
|
|
||||||
ti.Targets = &tgts
|
|
||||||
|
|
||||||
ls := map[string]interface{}{}
|
urls := []string{}
|
||||||
for k, v := range target.Labels.Elements() {
|
diags := target.URLs.ElementsAs(ctx, &urls, false)
|
||||||
ls[k], _ = conversion.ToString(ctx, v)
|
if diags.HasError() {
|
||||||
|
return nil, core.DiagsToError(diags)
|
||||||
}
|
}
|
||||||
ti.Labels = &ls
|
ti.Targets = &urls
|
||||||
|
|
||||||
|
labels := map[string]interface{}{}
|
||||||
|
for k, v := range target.Labels.Elements() {
|
||||||
|
labels[k], _ = conversion.ToString(ctx, v)
|
||||||
|
}
|
||||||
|
ti.Labels = &labels
|
||||||
t[i] = ti
|
t[i] = ti
|
||||||
}
|
}
|
||||||
sc.StaticConfigs = &t
|
sc.StaticConfigs = &t
|
||||||
|
|
||||||
return &sc, nil
|
return &sc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setDefaultsCreateScrapeConfig(sc *argus.CreateScrapeConfigPayload, model *Model) {
|
func setDefaultsCreateScrapeConfig(sc *argus.CreateScrapeConfigPayload, model *Model, saml2Model *saml2Model) {
|
||||||
if sc == nil {
|
if sc == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -646,7 +778,7 @@ func setDefaultsCreateScrapeConfig(sc *argus.CreateScrapeConfigPayload, model *M
|
||||||
sc.SampleLimit = utils.Ptr(float64(DefaultSampleLimit))
|
sc.SampleLimit = utils.Ptr(float64(DefaultSampleLimit))
|
||||||
}
|
}
|
||||||
// Make the API default more explicit by setting the field.
|
// Make the API default more explicit by setting the field.
|
||||||
if model.SAML2 == nil || model.SAML2.EnableURLParameters.IsNull() || model.SAML2.EnableURLParameters.IsUnknown() {
|
if saml2Model.EnableURLParameters.IsNull() || saml2Model.EnableURLParameters.IsUnknown() {
|
||||||
m := map[string]interface{}{}
|
m := map[string]interface{}{}
|
||||||
if sc.Params != nil {
|
if sc.Params != nil {
|
||||||
m = *sc.Params
|
m = *sc.Params
|
||||||
|
|
@ -660,7 +792,7 @@ func setDefaultsCreateScrapeConfig(sc *argus.CreateScrapeConfigPayload, model *M
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func toUpdatePayload(ctx context.Context, model *Model) (*argus.UpdateScrapeConfigPayload, error) {
|
func toUpdatePayload(ctx context.Context, model *Model, saml2Model *saml2Model, basicAuthModel *basicAuthModel, targetsModel []targetModel) (*argus.UpdateScrapeConfigPayload, error) {
|
||||||
if model == nil {
|
if model == nil {
|
||||||
return nil, fmt.Errorf("nil model")
|
return nil, fmt.Errorf("nil model")
|
||||||
}
|
}
|
||||||
|
|
@ -675,32 +807,36 @@ func toUpdatePayload(ctx context.Context, model *Model) (*argus.UpdateScrapeConf
|
||||||
}
|
}
|
||||||
setDefaultsUpdateScrapeConfig(&sc, model)
|
setDefaultsUpdateScrapeConfig(&sc, model)
|
||||||
|
|
||||||
if model.SAML2 != nil && !model.SAML2.EnableURLParameters.ValueBool() {
|
if !saml2Model.EnableURLParameters.IsNull() && !saml2Model.EnableURLParameters.IsUnknown() {
|
||||||
m := make(map[string]interface{})
|
m := make(map[string]interface{})
|
||||||
if sc.Params != nil {
|
if sc.Params != nil {
|
||||||
m = *sc.Params
|
m = *sc.Params
|
||||||
}
|
}
|
||||||
m["saml2"] = []string{"disabled"}
|
if saml2Model.EnableURLParameters.ValueBool() {
|
||||||
|
m["saml2"] = []string{"enabled"}
|
||||||
|
} else {
|
||||||
|
m["saml2"] = []string{"disabled"}
|
||||||
|
}
|
||||||
sc.Params = &m
|
sc.Params = &m
|
||||||
}
|
}
|
||||||
|
|
||||||
if model.BasicAuth != nil {
|
if sc.BasicAuth == nil && !basicAuthModel.Username.IsNull() && !basicAuthModel.Password.IsNull() {
|
||||||
if sc.BasicAuth == nil {
|
sc.BasicAuth = &argus.CreateScrapeConfigPayloadBasicAuth{
|
||||||
sc.BasicAuth = &argus.CreateScrapeConfigPayloadBasicAuth{
|
Username: conversion.StringValueToPointer(basicAuthModel.Username),
|
||||||
Username: conversion.StringValueToPointer(model.BasicAuth.Username),
|
Password: conversion.StringValueToPointer(basicAuthModel.Password),
|
||||||
Password: conversion.StringValueToPointer(model.BasicAuth.Password),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t := make([]argus.UpdateScrapeConfigPayloadStaticConfigsInner, len(model.Targets))
|
t := make([]argus.UpdateScrapeConfigPayloadStaticConfigsInner, len(targetsModel))
|
||||||
for i, target := range model.Targets {
|
for i, target := range targetsModel {
|
||||||
ti := argus.UpdateScrapeConfigPayloadStaticConfigsInner{}
|
ti := argus.UpdateScrapeConfigPayloadStaticConfigsInner{}
|
||||||
tgts := []string{}
|
|
||||||
for _, v := range target.URLs {
|
urls := []string{}
|
||||||
tgts = append(tgts, v.ValueString())
|
diags := target.URLs.ElementsAs(ctx, &urls, false)
|
||||||
|
if diags.HasError() {
|
||||||
|
return nil, core.DiagsToError(diags)
|
||||||
}
|
}
|
||||||
ti.Targets = &tgts
|
ti.Targets = &urls
|
||||||
|
|
||||||
ls := map[string]interface{}{}
|
ls := map[string]interface{}{}
|
||||||
for k, v := range target.Labels.Elements() {
|
for k, v := range target.Labels.Elements() {
|
||||||
|
|
@ -710,6 +846,7 @@ func toUpdatePayload(ctx context.Context, model *Model) (*argus.UpdateScrapeConf
|
||||||
t[i] = ti
|
t[i] = ti
|
||||||
}
|
}
|
||||||
sc.StaticConfigs = &t
|
sc.StaticConfigs = &t
|
||||||
|
|
||||||
return &sc, nil
|
return &sc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,9 +32,9 @@ func TestMapFields(t *testing.T) {
|
||||||
Scheme: types.StringNull(),
|
Scheme: types.StringNull(),
|
||||||
ScrapeInterval: types.StringNull(),
|
ScrapeInterval: types.StringNull(),
|
||||||
ScrapeTimeout: types.StringNull(),
|
ScrapeTimeout: types.StringNull(),
|
||||||
SAML2: nil,
|
SAML2: types.ObjectNull(saml2Types),
|
||||||
BasicAuth: nil,
|
BasicAuth: types.ObjectNull(basicAuthTypes),
|
||||||
Targets: []Target{},
|
Targets: types.ListNull(types.ObjectType{AttrTypes: targetTypes}),
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
|
|
@ -77,32 +77,32 @@ func TestMapFields(t *testing.T) {
|
||||||
ScrapeInterval: types.StringValue("1"),
|
ScrapeInterval: types.StringValue("1"),
|
||||||
ScrapeTimeout: types.StringValue("2"),
|
ScrapeTimeout: types.StringValue("2"),
|
||||||
SampleLimit: types.Int64Value(17),
|
SampleLimit: types.Int64Value(17),
|
||||||
SAML2: &SAML2{
|
SAML2: types.ObjectValueMust(saml2Types, map[string]attr.Value{
|
||||||
EnableURLParameters: types.BoolValue(false),
|
"enable_url_parameters": types.BoolValue(false),
|
||||||
},
|
}),
|
||||||
BasicAuth: &BasicAuth{
|
BasicAuth: types.ObjectValueMust(basicAuthTypes, map[string]attr.Value{
|
||||||
Username: types.StringValue("u"),
|
"username": types.StringValue("u"),
|
||||||
Password: types.StringValue("p"),
|
"password": types.StringValue("p"),
|
||||||
},
|
}),
|
||||||
Targets: []Target{
|
Targets: types.ListValueMust(types.ObjectType{AttrTypes: targetTypes}, []attr.Value{
|
||||||
{
|
types.ObjectValueMust(targetTypes, map[string]attr.Value{
|
||||||
URLs: []types.String{types.StringValue("url1")},
|
"urls": types.ListValueMust(types.StringType, []attr.Value{types.StringValue("url1")}),
|
||||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
"labels": types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||||
"k1": types.StringValue("v1"),
|
"k1": types.StringValue("v1"),
|
||||||
}),
|
}),
|
||||||
},
|
}),
|
||||||
{
|
types.ObjectValueMust(targetTypes, map[string]attr.Value{
|
||||||
URLs: []types.String{types.StringValue("url1"), types.StringValue("url3")},
|
"urls": types.ListValueMust(types.StringType, []attr.Value{types.StringValue("url1"), types.StringValue("url3")}),
|
||||||
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
|
"labels": types.MapValueMust(types.StringType, map[string]attr.Value{
|
||||||
"k2": types.StringValue("v2"),
|
"k2": types.StringValue("v2"),
|
||||||
"k3": types.StringValue("v3"),
|
"k3": types.StringValue("v3"),
|
||||||
}),
|
}),
|
||||||
},
|
}),
|
||||||
{
|
types.ObjectValueMust(targetTypes, map[string]attr.Value{
|
||||||
URLs: []types.String{},
|
"urls": types.ListValueMust(types.StringType, []attr.Value{}),
|
||||||
Labels: types.MapNull(types.StringType),
|
"labels": types.MapNull(types.StringType),
|
||||||
},
|
}),
|
||||||
},
|
}),
|
||||||
},
|
},
|
||||||
isValid: true,
|
isValid: true,
|
||||||
},
|
},
|
||||||
|
|
@ -125,7 +125,7 @@ func TestMapFields(t *testing.T) {
|
||||||
ProjectId: tt.expected.ProjectId,
|
ProjectId: tt.expected.ProjectId,
|
||||||
InstanceId: tt.expected.InstanceId,
|
InstanceId: tt.expected.InstanceId,
|
||||||
}
|
}
|
||||||
err := mapFields(tt.input, state)
|
err := mapFields(context.Background(), tt.input, state)
|
||||||
if !tt.isValid && err == nil {
|
if !tt.isValid && err == nil {
|
||||||
t.Fatalf("Should have failed")
|
t.Fatalf("Should have failed")
|
||||||
}
|
}
|
||||||
|
|
@ -144,16 +144,22 @@ func TestMapFields(t *testing.T) {
|
||||||
|
|
||||||
func TestToCreatePayload(t *testing.T) {
|
func TestToCreatePayload(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
description string
|
description string
|
||||||
input *Model
|
input *Model
|
||||||
expected *argus.CreateScrapeConfigPayload
|
inputSAML2 *saml2Model
|
||||||
isValid bool
|
inputBasicAuth *basicAuthModel
|
||||||
|
inputTargets []targetModel
|
||||||
|
expected *argus.CreateScrapeConfigPayload
|
||||||
|
isValid bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"basic_ok",
|
"basic_ok",
|
||||||
&Model{
|
&Model{
|
||||||
MetricsPath: types.StringValue("/metrics"),
|
MetricsPath: types.StringValue("/metrics"),
|
||||||
},
|
},
|
||||||
|
&saml2Model{},
|
||||||
|
&basicAuthModel{},
|
||||||
|
[]targetModel{},
|
||||||
&argus.CreateScrapeConfigPayload{
|
&argus.CreateScrapeConfigPayload{
|
||||||
MetricsPath: utils.Ptr("/metrics"),
|
MetricsPath: utils.Ptr("/metrics"),
|
||||||
// Defaults
|
// Defaults
|
||||||
|
|
@ -167,20 +173,126 @@ func TestToCreatePayload(t *testing.T) {
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ok",
|
"ok - false enable_url_parameters",
|
||||||
&Model{
|
&Model{
|
||||||
MetricsPath: types.StringValue("/metrics"),
|
MetricsPath: types.StringValue("/metrics"),
|
||||||
Name: types.StringValue("Name"),
|
Name: types.StringValue("Name"),
|
||||||
},
|
},
|
||||||
|
&saml2Model{
|
||||||
|
EnableURLParameters: types.BoolValue(false),
|
||||||
|
},
|
||||||
|
&basicAuthModel{},
|
||||||
|
[]targetModel{},
|
||||||
&argus.CreateScrapeConfigPayload{
|
&argus.CreateScrapeConfigPayload{
|
||||||
MetricsPath: utils.Ptr("/metrics"),
|
MetricsPath: utils.Ptr("/metrics"),
|
||||||
JobName: utils.Ptr("Name"),
|
JobName: utils.Ptr("Name"),
|
||||||
|
Params: &map[string]any{"saml2": []string{"disabled"}},
|
||||||
// Defaults
|
// Defaults
|
||||||
Scheme: utils.Ptr("https"),
|
Scheme: utils.Ptr("https"),
|
||||||
ScrapeInterval: utils.Ptr("5m"),
|
ScrapeInterval: utils.Ptr("5m"),
|
||||||
ScrapeTimeout: utils.Ptr("2m"),
|
ScrapeTimeout: utils.Ptr("2m"),
|
||||||
SampleLimit: utils.Ptr(float64(5000)),
|
SampleLimit: utils.Ptr(float64(5000)),
|
||||||
StaticConfigs: &[]argus.CreateScrapeConfigPayloadStaticConfigsInner{},
|
StaticConfigs: &[]argus.CreateScrapeConfigPayloadStaticConfigsInner{},
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ok - true enable_url_parameters",
|
||||||
|
&Model{
|
||||||
|
MetricsPath: types.StringValue("/metrics"),
|
||||||
|
Name: types.StringValue("Name"),
|
||||||
|
},
|
||||||
|
&saml2Model{
|
||||||
|
EnableURLParameters: types.BoolValue(true),
|
||||||
|
},
|
||||||
|
&basicAuthModel{},
|
||||||
|
[]targetModel{},
|
||||||
|
&argus.CreateScrapeConfigPayload{
|
||||||
|
MetricsPath: utils.Ptr("/metrics"),
|
||||||
|
JobName: utils.Ptr("Name"),
|
||||||
|
Params: &map[string]any{"saml2": []string{"enabled"}},
|
||||||
|
// Defaults
|
||||||
|
Scheme: utils.Ptr("https"),
|
||||||
|
ScrapeInterval: utils.Ptr("5m"),
|
||||||
|
ScrapeTimeout: utils.Ptr("2m"),
|
||||||
|
SampleLimit: utils.Ptr(float64(5000)),
|
||||||
|
StaticConfigs: &[]argus.CreateScrapeConfigPayloadStaticConfigsInner{},
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ok - with basic auth",
|
||||||
|
&Model{
|
||||||
|
MetricsPath: types.StringValue("/metrics"),
|
||||||
|
Name: types.StringValue("Name"),
|
||||||
|
},
|
||||||
|
&saml2Model{},
|
||||||
|
&basicAuthModel{
|
||||||
|
Username: types.StringValue("u"),
|
||||||
|
Password: types.StringValue("p"),
|
||||||
|
},
|
||||||
|
[]targetModel{},
|
||||||
|
&argus.CreateScrapeConfigPayload{
|
||||||
|
MetricsPath: utils.Ptr("/metrics"),
|
||||||
|
JobName: utils.Ptr("Name"),
|
||||||
|
BasicAuth: &argus.CreateScrapeConfigPayloadBasicAuth{
|
||||||
|
Username: utils.Ptr("u"),
|
||||||
|
Password: utils.Ptr("p"),
|
||||||
|
},
|
||||||
|
// Defaults
|
||||||
|
Scheme: utils.Ptr("https"),
|
||||||
|
ScrapeInterval: utils.Ptr("5m"),
|
||||||
|
ScrapeTimeout: utils.Ptr("2m"),
|
||||||
|
SampleLimit: utils.Ptr(float64(5000)),
|
||||||
|
StaticConfigs: &[]argus.CreateScrapeConfigPayloadStaticConfigsInner{},
|
||||||
|
Params: &map[string]any{"saml2": []string{"enabled"}},
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ok - with targets",
|
||||||
|
&Model{
|
||||||
|
MetricsPath: types.StringValue("/metrics"),
|
||||||
|
Name: types.StringValue("Name"),
|
||||||
|
},
|
||||||
|
&saml2Model{},
|
||||||
|
&basicAuthModel{},
|
||||||
|
[]targetModel{
|
||||||
|
{
|
||||||
|
URLs: types.ListValueMust(types.StringType, []attr.Value{types.StringValue("url1")}),
|
||||||
|
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{"k1": types.StringValue("v1")}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URLs: types.ListValueMust(types.StringType, []attr.Value{types.StringValue("url1"), types.StringValue("url3")}),
|
||||||
|
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{"k2": types.StringValue("v2"), "k3": types.StringValue("v3")}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URLs: types.ListValueMust(types.StringType, []attr.Value{}),
|
||||||
|
Labels: types.MapNull(types.StringType),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&argus.CreateScrapeConfigPayload{
|
||||||
|
MetricsPath: utils.Ptr("/metrics"),
|
||||||
|
JobName: utils.Ptr("Name"),
|
||||||
|
StaticConfigs: &[]argus.CreateScrapeConfigPayloadStaticConfigsInner{
|
||||||
|
{
|
||||||
|
Targets: &[]string{"url1"},
|
||||||
|
Labels: &map[string]interface{}{"k1": "v1"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Targets: &[]string{"url1", "url3"},
|
||||||
|
Labels: &map[string]interface{}{"k2": "v2", "k3": "v3"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Targets: &[]string{},
|
||||||
|
Labels: &map[string]interface{}{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// Defaults
|
||||||
|
Scheme: utils.Ptr("https"),
|
||||||
|
ScrapeInterval: utils.Ptr("5m"),
|
||||||
|
ScrapeTimeout: utils.Ptr("2m"),
|
||||||
|
SampleLimit: utils.Ptr(float64(5000)),
|
||||||
Params: &map[string]any{"saml2": []string{"enabled"}},
|
Params: &map[string]any{"saml2": []string{"enabled"}},
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
|
|
@ -189,12 +301,15 @@ func TestToCreatePayload(t *testing.T) {
|
||||||
"nil_model",
|
"nil_model",
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.description, func(t *testing.T) {
|
t.Run(tt.description, func(t *testing.T) {
|
||||||
output, err := toCreatePayload(context.Background(), tt.input)
|
output, err := toCreatePayload(context.Background(), tt.input, tt.inputSAML2, tt.inputBasicAuth, tt.inputTargets)
|
||||||
if !tt.isValid && err == nil {
|
if !tt.isValid && err == nil {
|
||||||
t.Fatalf("Should have failed")
|
t.Fatalf("Should have failed")
|
||||||
}
|
}
|
||||||
|
|
@ -213,16 +328,22 @@ func TestToCreatePayload(t *testing.T) {
|
||||||
|
|
||||||
func TestToUpdatePayload(t *testing.T) {
|
func TestToUpdatePayload(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
description string
|
description string
|
||||||
input *Model
|
input *Model
|
||||||
expected *argus.UpdateScrapeConfigPayload
|
inputSAML2 *saml2Model
|
||||||
isValid bool
|
basicAuthModel *basicAuthModel
|
||||||
|
inputTargets []targetModel
|
||||||
|
expected *argus.UpdateScrapeConfigPayload
|
||||||
|
isValid bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"basic_ok",
|
"basic_ok",
|
||||||
&Model{
|
&Model{
|
||||||
MetricsPath: types.StringValue("/metrics"),
|
MetricsPath: types.StringValue("/metrics"),
|
||||||
},
|
},
|
||||||
|
&saml2Model{},
|
||||||
|
&basicAuthModel{},
|
||||||
|
[]targetModel{},
|
||||||
&argus.UpdateScrapeConfigPayload{
|
&argus.UpdateScrapeConfigPayload{
|
||||||
MetricsPath: utils.Ptr("/metrics"),
|
MetricsPath: utils.Ptr("/metrics"),
|
||||||
// Defaults
|
// Defaults
|
||||||
|
|
@ -235,11 +356,16 @@ func TestToUpdatePayload(t *testing.T) {
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ok",
|
"ok - true enable_url_parameters",
|
||||||
&Model{
|
&Model{
|
||||||
MetricsPath: types.StringValue("/metrics"),
|
MetricsPath: types.StringValue("/metrics"),
|
||||||
Scheme: types.StringValue("http"),
|
Scheme: types.StringValue("http"),
|
||||||
},
|
},
|
||||||
|
&saml2Model{
|
||||||
|
EnableURLParameters: types.BoolValue(true),
|
||||||
|
},
|
||||||
|
&basicAuthModel{},
|
||||||
|
[]targetModel{},
|
||||||
&argus.UpdateScrapeConfigPayload{
|
&argus.UpdateScrapeConfigPayload{
|
||||||
MetricsPath: utils.Ptr("/metrics"),
|
MetricsPath: utils.Ptr("/metrics"),
|
||||||
// Defaults
|
// Defaults
|
||||||
|
|
@ -248,6 +374,103 @@ func TestToUpdatePayload(t *testing.T) {
|
||||||
ScrapeTimeout: utils.Ptr("2m"),
|
ScrapeTimeout: utils.Ptr("2m"),
|
||||||
SampleLimit: utils.Ptr(float64(5000)),
|
SampleLimit: utils.Ptr(float64(5000)),
|
||||||
StaticConfigs: &[]argus.UpdateScrapeConfigPayloadStaticConfigsInner{},
|
StaticConfigs: &[]argus.UpdateScrapeConfigPayloadStaticConfigsInner{},
|
||||||
|
Params: &map[string]any{"saml2": []string{"enabled"}},
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ok - false enable_url_parameters",
|
||||||
|
&Model{
|
||||||
|
MetricsPath: types.StringValue("/metrics"),
|
||||||
|
Scheme: types.StringValue("http"),
|
||||||
|
},
|
||||||
|
&saml2Model{
|
||||||
|
EnableURLParameters: types.BoolValue(false),
|
||||||
|
},
|
||||||
|
&basicAuthModel{},
|
||||||
|
[]targetModel{},
|
||||||
|
&argus.UpdateScrapeConfigPayload{
|
||||||
|
MetricsPath: utils.Ptr("/metrics"),
|
||||||
|
// Defaults
|
||||||
|
Scheme: utils.Ptr("http"),
|
||||||
|
ScrapeInterval: utils.Ptr("5m"),
|
||||||
|
ScrapeTimeout: utils.Ptr("2m"),
|
||||||
|
SampleLimit: utils.Ptr(float64(5000)),
|
||||||
|
StaticConfigs: &[]argus.UpdateScrapeConfigPayloadStaticConfigsInner{},
|
||||||
|
Params: &map[string]any{"saml2": []string{"disabled"}},
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ok - with basic auth",
|
||||||
|
&Model{
|
||||||
|
MetricsPath: types.StringValue("/metrics"),
|
||||||
|
Name: types.StringValue("Name"),
|
||||||
|
},
|
||||||
|
&saml2Model{},
|
||||||
|
&basicAuthModel{
|
||||||
|
Username: types.StringValue("u"),
|
||||||
|
Password: types.StringValue("p"),
|
||||||
|
},
|
||||||
|
[]targetModel{},
|
||||||
|
&argus.UpdateScrapeConfigPayload{
|
||||||
|
MetricsPath: utils.Ptr("/metrics"),
|
||||||
|
BasicAuth: &argus.CreateScrapeConfigPayloadBasicAuth{
|
||||||
|
Username: utils.Ptr("u"),
|
||||||
|
Password: utils.Ptr("p"),
|
||||||
|
},
|
||||||
|
// Defaults
|
||||||
|
Scheme: utils.Ptr("https"),
|
||||||
|
ScrapeInterval: utils.Ptr("5m"),
|
||||||
|
ScrapeTimeout: utils.Ptr("2m"),
|
||||||
|
SampleLimit: utils.Ptr(float64(5000)),
|
||||||
|
StaticConfigs: &[]argus.UpdateScrapeConfigPayloadStaticConfigsInner{},
|
||||||
|
},
|
||||||
|
true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ok - with targets",
|
||||||
|
&Model{
|
||||||
|
MetricsPath: types.StringValue("/metrics"),
|
||||||
|
Name: types.StringValue("Name"),
|
||||||
|
},
|
||||||
|
&saml2Model{},
|
||||||
|
&basicAuthModel{},
|
||||||
|
[]targetModel{
|
||||||
|
{
|
||||||
|
URLs: types.ListValueMust(types.StringType, []attr.Value{types.StringValue("url1")}),
|
||||||
|
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{"k1": types.StringValue("v1")}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URLs: types.ListValueMust(types.StringType, []attr.Value{types.StringValue("url1"), types.StringValue("url3")}),
|
||||||
|
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{"k2": types.StringValue("v2"), "k3": types.StringValue("v3")}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URLs: types.ListValueMust(types.StringType, []attr.Value{}),
|
||||||
|
Labels: types.MapNull(types.StringType),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&argus.UpdateScrapeConfigPayload{
|
||||||
|
MetricsPath: utils.Ptr("/metrics"),
|
||||||
|
StaticConfigs: &[]argus.UpdateScrapeConfigPayloadStaticConfigsInner{
|
||||||
|
{
|
||||||
|
Targets: &[]string{"url1"},
|
||||||
|
Labels: &map[string]interface{}{"k1": "v1"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Targets: &[]string{"url1", "url3"},
|
||||||
|
Labels: &map[string]interface{}{"k2": "v2", "k3": "v3"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Targets: &[]string{},
|
||||||
|
Labels: &map[string]interface{}{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// Defaults
|
||||||
|
Scheme: utils.Ptr("https"),
|
||||||
|
ScrapeInterval: utils.Ptr("5m"),
|
||||||
|
ScrapeTimeout: utils.Ptr("2m"),
|
||||||
|
SampleLimit: utils.Ptr(float64(5000)),
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
|
|
@ -255,12 +478,15 @@ func TestToUpdatePayload(t *testing.T) {
|
||||||
"nil_model",
|
"nil_model",
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.description, func(t *testing.T) {
|
t.Run(tt.description, func(t *testing.T) {
|
||||||
output, err := toUpdatePayload(context.Background(), tt.input)
|
output, err := toUpdatePayload(context.Background(), tt.input, tt.inputSAML2, tt.basicAuthModel, tt.inputTargets)
|
||||||
if !tt.isValid && err == nil {
|
if !tt.isValid && err == nil {
|
||||||
t.Fatalf("Should have failed")
|
t.Fatalf("Should have failed")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue