From acab86c210f7c70399bd3b43e265fdba0874661f Mon Sep 17 00:00:00 2001 From: Or Novogroder <108669655+OrNovo@users.noreply.github.com> Date: Wed, 31 Jan 2024 12:13:42 +0200 Subject: [PATCH] patch - adding validation for 'env' field (#194) --- CHANGELOG.md | 6 +++++- coralogix/provider.go | 43 ++++++++++++++++++++++++++++++++----------- docs/resources/slo.md | 35 ++++++++++++++++++----------------- 3 files changed, 55 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dca84806..a40a6f29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -426,4 +426,8 @@ New Features: Bug fixing: #### resource/coralogix_team -* fixing log message when for permission denied error. \ No newline at end of file +* fixing log message when for permission denied error. + +## Release 1.11.2 +Bug fixing: +* adding validation for `env` field. \ No newline at end of file diff --git a/coralogix/provider.go b/coralogix/provider.go index d7e2bd86..e15542f0 100644 --- a/coralogix/provider.go +++ b/coralogix/provider.go @@ -5,6 +5,10 @@ import ( "fmt" "os" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "golang.org/x/exp/slices" "terraform-provider-coralogix/coralogix/clientset" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -39,17 +43,17 @@ func OldProvider() *oldSchema.Provider { Optional: true, //ForceNew: true, //DefaultFunc: oldSchema.EnvDefaultFunc("CORALOGIX_ENV", nil), - //ValidateFunc: validation.StringInSlice(validEnvs, false), - Description: fmt.Sprintf("The Coralogix API environment. can be one of %q. environment variable 'CORALOGIX_ENV' can be defined instead.", validEnvs), - //ConflictsWith: []string{"domain"}, + ValidateFunc: validation.StringInSlice(validEnvs, false), + Description: fmt.Sprintf("The Coralogix API environment. can be one of %q. environment variable 'CORALOGIX_ENV' can be defined instead.", validEnvs), + ConflictsWith: []string{"domain"}, }, "domain": { Type: oldSchema.TypeString, Optional: true, //ForceNew: true, //DefaultFunc: oldSchema.EnvDefaultFunc("CORALOGIX_DOMAIN", nil), - Description: "The Coralogix domain. Conflict With 'env'. environment variable 'CORALOGIX_DOMAIN' can be defined instead.", - //ConflictsWith: []string{"env"}, + Description: "The Coralogix domain. Conflict With 'env'. environment variable 'CORALOGIX_DOMAIN' can be defined instead.", + ConflictsWith: []string{"env"}, }, "api_key": { Type: oldSchema.TypeString, @@ -87,11 +91,19 @@ func OldProvider() *oldSchema.Provider { ConfigureContextFunc: func(context context.Context, d *oldSchema.ResourceData) (interface{}, diag.Diagnostics) { var targetUrl string if env, ok := d.GetOk("env"); ok && env.(string) != "" { - targetUrl = envToGrpcUrl[env.(string)] + if url, ok := envToGrpcUrl[env.(string)]; !ok { + return nil, diag.Errorf("The Coralogix env must be one of %q", validEnvs) + } else { + targetUrl = url + } } else if domain, ok := d.GetOk("domain"); ok && domain.(string) != "" { targetUrl = fmt.Sprintf("ng-api-grpc.%s:443", domain) } else if env = os.Getenv("CORALOGIX_ENV"); env != "" { - targetUrl = envToGrpcUrl[env.(string)] + if url, ok := envToGrpcUrl[env.(string)]; !ok { + return nil, diag.Errorf("The Coralogix env must be one of %q", validEnvs) + } else { + targetUrl = url + } } else if domain = os.Getenv("CORALOGIX_DOMAIN"); domain != "" { targetUrl = fmt.Sprintf("ng-api-grpc.%s:443", domain) } else { @@ -142,12 +154,19 @@ func (p *coralogixProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ "env": schema.StringAttribute{ - Optional: true, - Description: fmt.Sprintf("The Coralogix API environment. can be one of %q. environment variable 'CORALOGIX_ENV' can be defined instead.", validEnvs), + Optional: true, + Validators: []validator.String{ + stringvalidator.OneOf(validEnvs...), + stringvalidator.ConflictsWith(path.MatchRelative().AtParent().AtName("domain")), + }, + MarkdownDescription: fmt.Sprintf("The Coralogix API environment. can be one of %q. environment variable 'CORALOGIX_ENV' can be defined instead.", validEnvs), }, "domain": schema.StringAttribute{ - Optional: true, - Description: "The Coralogix domain. Conflict With 'env'. environment variable 'CORALOGIX_DOMAIN' can be defined instead.", + Optional: true, + Validators: []validator.String{ + stringvalidator.ConflictsWith(path.MatchRelative().AtParent().AtName("domain")), + }, + MarkdownDescription: "The Coralogix domain. Conflict With 'env'. environment variable 'CORALOGIX_DOMAIN' can be defined instead.", }, "api_key": schema.StringAttribute{ Optional: true, @@ -264,6 +283,8 @@ func (p *coralogixProvider) Configure(ctx context.Context, req provider.Configur "Only one of \"env\" need to be set."+ "ensure CORALOGIX_ENV and CORALOGIX_DOMAIN are not set together as well.", ) + } else if domain == "" && !slices.Contains(validEnvs, env) { + resp.Diagnostics.AddAttributeError(path.Root("env"), "Invalid Coralogix env", fmt.Sprintf("The Coralogix env must be one of %q", validEnvs)) } if apiKey == "" && orgKey == "" { diff --git a/docs/resources/slo.md b/docs/resources/slo.md index a5b30490..42dce35b 100644 --- a/docs/resources/slo.md +++ b/docs/resources/slo.md @@ -20,29 +20,30 @@ Note: Only Administrators can manage SLOs. ```hcl resource "coralogix_slo" "example" { - name = "example" - period = "30_days" - service_name = "example" - target_percentage = 99.9 - type = "error" - description = "example" - name = "coralogix_slo_example" - service_name = "service_name" - description = "description" - target_percentage = 30 - type = "latency" + name = "coralogix_slo_example" + service_name = "service_name" + description = "description" + target_percentage = 30 + type = "error" + period = "7_days" +} + +resource "coralogix_slo" "example_2" { + name = "coralogix_slo_example" + service_name = "service_name" + description = "description" + target_percentage = 30 + type = "latency" threshold_microseconds = 1000000 - threshold_symbol_type = "greater" - period = "7_days" - filters = [ + threshold_symbol_type = "greater" + period = "7_days" + filters = [ { - field = "severity" + field = "severity" compare_type = "is" field_values = ["error", "warning"] }, ] - threshold_microseconds = 1000000 - threshold_symbol_type = "greater" } ```