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:
João Palet 2024-04-29 15:18:13 +01:00 committed by GitHub
parent 8ddcfc8e43
commit e48deaff59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 481 additions and 118 deletions

View file

@ -32,9 +32,9 @@ func TestMapFields(t *testing.T) {
Scheme: types.StringNull(),
ScrapeInterval: types.StringNull(),
ScrapeTimeout: types.StringNull(),
SAML2: nil,
BasicAuth: nil,
Targets: []Target{},
SAML2: types.ObjectNull(saml2Types),
BasicAuth: types.ObjectNull(basicAuthTypes),
Targets: types.ListNull(types.ObjectType{AttrTypes: targetTypes}),
},
true,
},
@ -77,32 +77,32 @@ func TestMapFields(t *testing.T) {
ScrapeInterval: types.StringValue("1"),
ScrapeTimeout: types.StringValue("2"),
SampleLimit: types.Int64Value(17),
SAML2: &SAML2{
EnableURLParameters: types.BoolValue(false),
},
BasicAuth: &BasicAuth{
Username: types.StringValue("u"),
Password: types.StringValue("p"),
},
Targets: []Target{
{
URLs: []types.String{types.StringValue("url1")},
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
SAML2: types.ObjectValueMust(saml2Types, map[string]attr.Value{
"enable_url_parameters": types.BoolValue(false),
}),
BasicAuth: types.ObjectValueMust(basicAuthTypes, map[string]attr.Value{
"username": types.StringValue("u"),
"password": types.StringValue("p"),
}),
Targets: types.ListValueMust(types.ObjectType{AttrTypes: targetTypes}, []attr.Value{
types.ObjectValueMust(targetTypes, map[string]attr.Value{
"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.String{types.StringValue("url1"), types.StringValue("url3")},
Labels: types.MapValueMust(types.StringType, map[string]attr.Value{
}),
types.ObjectValueMust(targetTypes, map[string]attr.Value{
"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.String{},
Labels: types.MapNull(types.StringType),
},
},
}),
types.ObjectValueMust(targetTypes, map[string]attr.Value{
"urls": types.ListValueMust(types.StringType, []attr.Value{}),
"labels": types.MapNull(types.StringType),
}),
}),
},
isValid: true,
},
@ -125,7 +125,7 @@ func TestMapFields(t *testing.T) {
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
}
err := mapFields(tt.input, state)
err := mapFields(context.Background(), tt.input, state)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
@ -144,16 +144,22 @@ func TestMapFields(t *testing.T) {
func TestToCreatePayload(t *testing.T) {
tests := []struct {
description string
input *Model
expected *argus.CreateScrapeConfigPayload
isValid bool
description string
input *Model
inputSAML2 *saml2Model
inputBasicAuth *basicAuthModel
inputTargets []targetModel
expected *argus.CreateScrapeConfigPayload
isValid bool
}{
{
"basic_ok",
&Model{
MetricsPath: types.StringValue("/metrics"),
},
&saml2Model{},
&basicAuthModel{},
[]targetModel{},
&argus.CreateScrapeConfigPayload{
MetricsPath: utils.Ptr("/metrics"),
// Defaults
@ -167,20 +173,126 @@ func TestToCreatePayload(t *testing.T) {
true,
},
{
"ok",
"ok - false enable_url_parameters",
&Model{
MetricsPath: types.StringValue("/metrics"),
Name: types.StringValue("Name"),
},
&saml2Model{
EnableURLParameters: types.BoolValue(false),
},
&basicAuthModel{},
[]targetModel{},
&argus.CreateScrapeConfigPayload{
MetricsPath: utils.Ptr("/metrics"),
JobName: utils.Ptr("Name"),
Params: &map[string]any{"saml2": []string{"disabled"}},
// Defaults
Scheme: utils.Ptr("https"),
ScrapeInterval: utils.Ptr("5m"),
ScrapeTimeout: utils.Ptr("2m"),
SampleLimit: utils.Ptr(float64(5000)),
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"}},
},
true,
@ -189,12 +301,15 @@ func TestToCreatePayload(t *testing.T) {
"nil_model",
nil,
nil,
nil,
nil,
nil,
false,
},
}
for _, tt := range tests {
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 {
t.Fatalf("Should have failed")
}
@ -213,16 +328,22 @@ func TestToCreatePayload(t *testing.T) {
func TestToUpdatePayload(t *testing.T) {
tests := []struct {
description string
input *Model
expected *argus.UpdateScrapeConfigPayload
isValid bool
description string
input *Model
inputSAML2 *saml2Model
basicAuthModel *basicAuthModel
inputTargets []targetModel
expected *argus.UpdateScrapeConfigPayload
isValid bool
}{
{
"basic_ok",
&Model{
MetricsPath: types.StringValue("/metrics"),
},
&saml2Model{},
&basicAuthModel{},
[]targetModel{},
&argus.UpdateScrapeConfigPayload{
MetricsPath: utils.Ptr("/metrics"),
// Defaults
@ -235,11 +356,16 @@ func TestToUpdatePayload(t *testing.T) {
true,
},
{
"ok",
"ok - true enable_url_parameters",
&Model{
MetricsPath: types.StringValue("/metrics"),
Scheme: types.StringValue("http"),
},
&saml2Model{
EnableURLParameters: types.BoolValue(true),
},
&basicAuthModel{},
[]targetModel{},
&argus.UpdateScrapeConfigPayload{
MetricsPath: utils.Ptr("/metrics"),
// Defaults
@ -248,6 +374,103 @@ func TestToUpdatePayload(t *testing.T) {
ScrapeTimeout: utils.Ptr("2m"),
SampleLimit: utils.Ptr(float64(5000)),
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,
},
@ -255,12 +478,15 @@ func TestToUpdatePayload(t *testing.T) {
"nil_model",
nil,
nil,
nil,
nil,
nil,
false,
},
}
for _, tt := range tests {
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 {
t.Fatalf("Should have failed")
}