From fdf8bda804496709428639a56ec651d253fbaf2b Mon Sep 17 00:00:00 2001 From: Garfield Lee Freeman Date: Thu, 6 Jun 2024 13:45:26 +0200 Subject: [PATCH] generate the provider --- docs/index.md | 4 +++ internal/provider/provider.go | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/docs/index.md b/docs/index.md index 56e1cfc..cd0b1f7 100644 --- a/docs/index.md +++ b/docs/index.md @@ -48,10 +48,14 @@ There are multiple ways to specify the provider's parameters. If overlapping va ### Optional - `auth_file` (String) The file path to the JSON file with auth creds for SCM. +- `auth_url` (String) The URL to send auth credentials to which will return a JWT. Default: `https://auth.apps.paloaltonetworks.com/auth/v1/oauth2/access_token`. Environment variable: `SCM_AUTH_URL`. JSON config file variable: `auth_url`. - `client_id` (String) The client ID for the connection. Environment variable: `SCM_CLIENT_ID`. JSON config file variable: `client_id`. - `client_secret` (String, Sensitive) The client secret for the connection. Environment variable: `SCM_CLIENT_SECRET`. JSON config file variable: `client_secret`. +- `headers` (Map of String) Custom HTTP headers to be sent with all API commands. Environment variable: `SCM_HEADERS`. JSON config file variable: `headers`. - `host` (String) The hostname of Strata Cloud Manager API. Default: `api.sase.paloaltonetworks.com`. Environment variable: `SCM_HOST`. JSON config file variable: `host`. - `logging` (String) The logging level of the provider and the underlying communication. Default: `quiet`. Environment variable: `SCM_LOGGING`. JSON config file variable: `logging`. +- `port` (Number) The port number to use for API commands, if non-standard for the given protocol. Environment variable: `SCM_PORT`. JSON config file variable: `port`. +- `protocol` (String) The protocol to use for SCM. This should be 'http' or 'https'. Default: `https`. Environment variable: `SCM_PROTOCOL`. JSON config file variable: `protocol`. - `scope` (String) The client scope. Environment variable: `SCM_SCOPE`. JSON config file variable: `scope`. diff --git a/internal/provider/provider.go b/internal/provider/provider.go index dd6d493..da19c73 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -27,7 +27,11 @@ type ScmProvider struct { // ScmProviderModel maps provider schema data to a Go type. type ScmProviderModel struct { + AuthUrl types.String `tfsdk:"auth_url"` + Protocol types.String `tfsdk:"protocol"` Host types.String `tfsdk:"host"` + Port types.Int64 `tfsdk:"port"` + Headers types.Map `tfsdk:"headers"` ClientId types.String `tfsdk:"client_id"` ClientSecret types.String `tfsdk:"client_secret"` Scope types.String `tfsdk:"scope"` @@ -46,6 +50,24 @@ func (p *ScmProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp * resp.Schema = schema.Schema{ Description: "Terraform provider to interact with Palo Alto Networks Strata Cloud Manager API.", Attributes: map[string]schema.Attribute{ + "auth_url": schema.StringAttribute{ + Description: ProviderParamDescription( + "The URL to send auth credentials to which will return a JWT.", + "https://auth.apps.paloaltonetworks.com/auth/v1/oauth2/access_token", + "SCM_AUTH_URL", + "auth_url", + ), + Optional: true, + }, + "protocol": schema.StringAttribute{ + Description: ProviderParamDescription( + "The protocol to use for SCM. This should be 'http' or 'https'.", + "https", + "SCM_PROTOCOL", + "protocol", + ), + Optional: true, + }, "host": schema.StringAttribute{ Description: ProviderParamDescription( "The hostname of Strata Cloud Manager API.", @@ -55,6 +77,25 @@ func (p *ScmProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp * ), Optional: true, }, + "port": schema.Int64Attribute{ + Description: ProviderParamDescription( + "The port number to use for API commands, if non-standard for the given protocol.", + "", + "SCM_PORT", + "port", + ), + Optional: true, + }, + "headers": schema.MapAttribute{ + Description: ProviderParamDescription( + "Custom HTTP headers to be sent with all API commands.", + "", + "SCM_HEADERS", + "headers", + ), + Optional: true, + ElementType: types.StringType, + }, "client_id": schema.StringAttribute{ Description: ProviderParamDescription( "The client ID for the connection.", @@ -116,8 +157,23 @@ func (p *ScmProvider) Configure(ctx context.Context, req provider.ConfigureReque } // Configure the client. + ht := make(map[string]types.String, len(config.Headers.Elements())) + resp.Diagnostics.Append(config.Headers.ElementsAs(ctx, &ht, false).Errors()...) + if resp.Diagnostics.HasError() { + return + } + + headers := make(map[string]string, len(ht)) + for hkey, hval := range ht { + headers[hkey] = hval.ValueString() + } + con := &sdk.Client{ + AuthUrl: config.AuthUrl.ValueString(), + Protocol: config.Protocol.ValueString(), Host: config.Host.ValueString(), + Port: int(config.Port.ValueInt64()), + Headers: headers, ClientId: config.ClientId.ValueString(), ClientSecret: config.ClientSecret.ValueString(), Scope: config.Scope.ValueString(),