Skip to content

Commit

Permalink
patch - adding validation for 'env' field (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrNovo authored Jan 31, 2024
1 parent 76b70f2 commit acab86c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,8 @@ New Features:

Bug fixing:
#### resource/coralogix_team
* fixing log message when for permission denied error.
* fixing log message when for permission denied error.

## Release 1.11.2
Bug fixing:
* adding validation for `env` field.
43 changes: 32 additions & 11 deletions coralogix/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 == "" {
Expand Down
35 changes: 18 additions & 17 deletions docs/resources/slo.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand Down

0 comments on commit acab86c

Please sign in to comment.