Skip to content

Commit

Permalink
⭐️ support custom endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Nov 3, 2023
1 parent f1fab32 commit 8761286
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ provider "mondoo" {
### Optional

- `credentials` (String) Either the path to or the contents of a service account key file in JSON format.
- `endpoint` (String) The endpoint url of the server to manage resources
- `region` (String) The default region to manage resources in.
- `space` (String) The default space to manage resources in.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ require (
github.com/hashicorp/copywrite v0.16.6
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-framework v1.4.2
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.19.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.5.1
go.mondoo.com/mondoo-go v0.0.0-20231003201540-9988d4953ca2
go.mondoo.com/mondoo-go v0.0.0-20231101113453-e876aab537f7
)

require (
Expand Down Expand Up @@ -105,7 +106,7 @@ require (
golang.org/x/exp v0.0.0-20230809150735-7b3493d9a819 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ github.com/hashicorp/terraform-plugin-docs v0.16.0 h1:UmxFr3AScl6Wged84jndJIfFcc
github.com/hashicorp/terraform-plugin-docs v0.16.0/go.mod h1:M3ZrlKBJAbPMtNOPwHicGi1c+hZUh7/g0ifT/z7TVfA=
github.com/hashicorp/terraform-plugin-framework v1.4.2 h1:P7a7VP1GZbjc4rv921Xy5OckzhoiO3ig6SGxwelD2sI=
github.com/hashicorp/terraform-plugin-framework v1.4.2/go.mod h1:GWl3InPFZi2wVQmdVnINPKys09s9mLmTZr95/ngLnbY=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 h1:HOjBuMbOEzl7snOdOoUfE2Jgeto6JOjLVQ39Ls2nksc=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0/go.mod h1:jfHGE/gzjxYz6XoUwi/aYiiKrJDeutQNUtGQXkaHklg=
github.com/hashicorp/terraform-plugin-go v0.19.0 h1:BuZx/6Cp+lkmiG0cOBk6Zps0Cb2tmqQpDM3iAtnhDQU=
github.com/hashicorp/terraform-plugin-go v0.19.0/go.mod h1:EhRSkEPNoylLQntYsk5KrDHTZJh9HQoumZXbOGOXmec=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down Expand Up @@ -496,8 +498,8 @@ github.com/zclconf/go-cty v1.13.3/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4
go.etcd.io/etcd/api/v3 v3.5.4/go.mod h1:5GB2vv4A4AOn3yk7MftYGHkUfGtDHnEraIjym4dYz5A=
go.etcd.io/etcd/client/pkg/v3 v3.5.4/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=
go.etcd.io/etcd/client/v3 v3.5.4/go.mod h1:ZaRkVgBZC+L+dLCjTcF1hRXpgZXQPOvnA/Ak/gq3kiY=
go.mondoo.com/mondoo-go v0.0.0-20231003201540-9988d4953ca2 h1:QZuJ/BB+b9IrEy9PYCe+WDKEHrZ8TvF9RMmmDrgkyvk=
go.mondoo.com/mondoo-go v0.0.0-20231003201540-9988d4953ca2/go.mod h1:5J966CXm/8xQQzMNpbG1jGfn5FEbscJI5Tt/V2CIQ5w=
go.mondoo.com/mondoo-go v0.0.0-20231101113453-e876aab537f7 h1:7PKGAAvPLLftvQhTM4/aWfwYLfzU/B9osqbqmWbQofE=
go.mondoo.com/mondoo-go v0.0.0-20231101113453-e876aab537f7/go.mod h1:vejsRmyreiLvCrKZDDnsoRD4n5S6/qLnmaHLXA3mn5Q=
go.mongodb.org/mongo-driver v1.10.0 h1:UtV6N5k14upNp4LTduX0QCufG124fSu25Wz9tu94GLg=
go.mongodb.org/mongo-driver v1.10.0/go.mod h1:wsihk0Kdgv8Kqu1Anit4sfK+22vSFbUrAVEYRhCXrA8=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
Expand Down Expand Up @@ -563,8 +565,8 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAG
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE=
golang.org/x/oauth2 v0.12.0 h1:smVPGxink+n1ZI5pkQa8y6fZT0RW0MgCO5bFpepy4B4=
golang.org/x/oauth2 v0.12.0/go.mod h1:A74bZ3aGXgCY0qaIC9Ahg6Lglin4AMAco8cIv9baba4=
golang.org/x/oauth2 v0.13.0 h1:jDDenyj+WgFtmV3zYVoi8aE2BwtXFLWOA67ZfNWftiY=
golang.org/x/oauth2 v0.13.0/go.mod h1:/JMhi4ZRXAf4HG9LiNmxvk+45+96RUlVThiH8FzNBn0=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
29 changes: 24 additions & 5 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ package provider
import (
"context"
"os"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
mondoov1 "go.mondoo.com/mondoo-go"
"go.mondoo.com/mondoo-go/option"
Expand All @@ -32,6 +35,7 @@ type MondooProviderModel struct {
Credentials types.String `tfsdk:"credentials"`
Space types.String `tfsdk:"space"`
Region types.String `tfsdk:"region"`
Endpoint types.String `tfsdk:"endpoint"`
}

func (p *MondooProvider) Metadata(ctx context.Context, req provider.MetadataRequest, resp *provider.MetadataResponse) {
Expand All @@ -53,6 +57,13 @@ func (p *MondooProvider) Schema(ctx context.Context, req provider.SchemaRequest,
"region": schema.StringAttribute{
MarkdownDescription: "The default region to manage resources in.",
Optional: true,
Validators: []validator.String{
stringvalidator.OneOf("us", "eu"),
},
},
"endpoint": schema.StringAttribute{
MarkdownDescription: "The endpoint url of the server to manage resources",
Optional: true,
},
},
}
Expand All @@ -79,11 +90,19 @@ func (p *MondooProvider) Configure(ctx context.Context, req provider.ConfigureRe

opts := []option.ClientOption{option.WithAPIToken(token)}

switch data.Region.String() {
case "us":
opts = append(opts, option.UseUSRegion())
case "eu":
opts = append(opts, option.UseEURegion())
if data.Endpoint.String() != "" {
url := data.Endpoint.ValueString()
if !strings.HasSuffix(url, "/query") {
url = url + "/query"
}
opts = append(opts, option.WithEndpoint(url))
} else {
switch data.Region.ValueString() {
case "eu":
opts = append(opts, option.UseEURegion())
default:
opts = append(opts, option.UseUSRegion())
}
}

client, err := mondoov1.NewClient(opts...)
Expand Down

0 comments on commit 8761286

Please sign in to comment.