Skip to content

Commit

Permalink
generate the provider
Browse files Browse the repository at this point in the history
  • Loading branch information
shinmog committed Jun 6, 2024
1 parent 0f91e34 commit fdf8bda
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.


Expand Down
56 changes: 56 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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.",
Expand All @@ -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.",
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit fdf8bda

Please sign in to comment.