Skip to content

Commit

Permalink
feat: metadata dastasource
Browse files Browse the repository at this point in the history
  • Loading branch information
fullykubed committed Dec 12, 2024
1 parent e99a50c commit 95086ad
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .copywrite.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ project {
copyright_year = 2024

header_ignore = [
"go/**",

# examples used within documentation (prose)
"examples/**",

Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ to an absolute path to the local copy of this provider repository on your local
The release process is configured according
to the [publishing guide provided by Hashicorp](https://developer.hashicorp.com/terraform/registry/providers/publishing)
and is based on the [terraform-provider-scaffolding-framework](https://github.com/hashicorp/terraform-provider-scaffolding-framework)
repository.
repository.

To cut a new release:

1. Run `go generate .` inside of the `tools` directory to update the documentation.
2. Commit your changes.
3. Tag the commit with a semver tag (e.g., `v0.0.1`).
4. Push the changes `git push --atomic origin main <tag>`.
29 changes: 29 additions & 0 deletions docs/data-sources/metadata.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "pf_metadata Data Source - pf"
subcategory: ""
description: |-
Provides metadata about the IaC deployment context.
---

# pf_metadata (Data Source)

Provides metadata about the IaC deployment context.



<!-- schema generated by tfplugindocs -->
## Schema

### Read-Only

- `environment` (String) The name of the environment that you are currently deploying infrastructure to
- `is_local` (Boolean) Whether the provider is being used a part of a local development deployment
- `kube_api_server` (String) The HTTPS address of the Kubernetes API server to which infrastructure is being deployed
- `kube_cluster_name` (String) The name of the Kubernetes cluster that you are currently deploying infrastructure to
- `kube_config_context` (String) The name of the context from kubeconfig file that is being used to deploy infrastructure
- `kube_config_path` (String) The path to the kubeconfig file that is being used to deploy infrastructure
- `region` (String) The name of the region that you are currently deploying infrastructure to
- `root_module` (String) The name of the root / top-level module that you are currently deploying infrastructure with
- `stack_commit` (String) The commit hash of the Panfactum Stack that you are currently using
- `stack_version` (String) The version of the Panfactum Stack that you are currently using
3 changes: 3 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ description: |-
- `environment` (String) The name of the environment that you are currently deploying infrastructure to
- `extra_tags` (Map of String) Extra tags to apply to all resources
- `is_local` (Boolean) Whether the provider is being used a part of a local development deployment
- `kube_api_server` (String) The HTTPS address of the Kubernetes API server to which infrastructure is being deployed
- `kube_cluster_name` (String) The name of the Kubernetes cluster that you are currently deploying infrastructure to
- `kube_config_context` (String) The name of the context from KUBE_CONFIG that is being used to deploy infrastructure
- `region` (String) The name of the region that you are currently deploying infrastructure to
- `root_module` (String) The name of the root / top-level module that you are currently deploying infrastructure with
- `stack_commit` (String) The commit hash of the Panfactum Stack that you are currently using
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/grpc v1.66.2 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 2 additions & 2 deletions provider/aws_tags_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewAWSTagsDataSource() datasource.DataSource {
}

type awsTagsDataSource struct {
ProviderData *PanfactumProviderModel
ProviderData *PanfactumProvider
}

type awsLabelsDataSourceModel struct {
Expand Down Expand Up @@ -67,7 +67,7 @@ func (d *awsTagsDataSource) Configure(ctx context.Context, req datasource.Config
return
}

data, ok := req.ProviderData.(*PanfactumProviderModel)
data, ok := req.ProviderData.(*PanfactumProvider)

if !ok {
resp.Diagnostics.AddError(
Expand Down
4 changes: 2 additions & 2 deletions provider/kube_labels_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewKubeLabelsDataSource() datasource.DataSource {
}

type kubeLabelsDataSource struct {
ProviderData *PanfactumProviderModel
ProviderData *PanfactumProvider
}

type kubeLabelsDataSourceModel struct {
Expand Down Expand Up @@ -61,7 +61,7 @@ func (d *kubeLabelsDataSource) Configure(ctx context.Context, req datasource.Con
return
}

data, ok := req.ProviderData.(*PanfactumProviderModel)
data, ok := req.ProviderData.(*PanfactumProvider)

if !ok {
resp.Diagnostics.AddError(
Expand Down
147 changes: 147 additions & 0 deletions provider/metadata_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: Apache-2.0

package provider

import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

/**************************************************************
Provider Definition
**************************************************************/

var _ datasource.DataSource = &metadataDataSource{}

func NewMetadataDataSource() datasource.DataSource {
return &metadataDataSource{}
}

type metadataDataSource struct {
ProviderData *PanfactumProvider
}

type metadataDataSourceModel struct {
Environment types.String `tfsdk:"environment"`
Region types.String `tfsdk:"region"`
RootModule types.String `tfsdk:"root_module"`
StackVersion types.String `tfsdk:"stack_version"`
StackCommit types.String `tfsdk:"stack_commit"`
IsLocal types.Bool `tfsdk:"is_local"`
KubeConfigPath types.String `tfsdk:"kube_config_path"`
KubeConfigContext types.String `tfsdk:"kube_config_context"`
KubeAPIServer types.String `tfsdk:"kube_api_server"`
KubeClusterName types.String `tfsdk:"kube_cluster_name"`
}

func (d *metadataDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_metadata"
}

func (d *metadataDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Provides metadata about the IaC deployment context.",
MarkdownDescription: "Provides metadata about the IaC deployment context.",

Attributes: map[string]schema.Attribute{
"environment": schema.StringAttribute{
Description: "The name of the environment that you are currently deploying infrastructure to",
MarkdownDescription: "The name of the environment that you are currently deploying infrastructure to",
Computed: true,
},
"region": schema.StringAttribute{
Description: "The name of the region that you are currently deploying infrastructure to",
MarkdownDescription: "The name of the region that you are currently deploying infrastructure to",
Computed: true,
},
"root_module": schema.StringAttribute{
Computed: true,
Description: "The name of the root / top-level module that you are currently deploying infrastructure with",
MarkdownDescription: "The name of the root / top-level module that you are currently deploying infrastructure with",
},
"stack_version": schema.StringAttribute{
Computed: true,
Description: "The version of the Panfactum Stack that you are currently using",
MarkdownDescription: "The version of the Panfactum Stack that you are currently using",
},
"stack_commit": schema.StringAttribute{
Computed: true,
Description: "The commit hash of the Panfactum Stack that you are currently using",
MarkdownDescription: "The commit hash of the Panfactum Stack that you are currently using",
},
"is_local": schema.BoolAttribute{
Computed: true,
Description: "Whether the provider is being used a part of a local development deployment",
MarkdownDescription: "Whether the provider is being used a part of a local development deployment",
},
"kube_config_path": schema.StringAttribute{
Description: "The path to the kubeconfig file that is being used to deploy infrastructure",
MarkdownDescription: "The path to the kubeconfig file that is being used to deploy infrastructure",
Computed: true,
},
"kube_config_context": schema.StringAttribute{
Description: "The name of the context from the kubeconfig file that is being used to deploy infrastructure",
MarkdownDescription: "The name of the context from kubeconfig file that is being used to deploy infrastructure",
Computed: true,
},
"kube_api_server": schema.StringAttribute{
Description: "The HTTPS address of the Kubernetes API server to which infrastructure is being deployed",
MarkdownDescription: "The HTTPS address of the Kubernetes API server to which infrastructure is being deployed",
Computed: true,
},
"kube_cluster_name": schema.StringAttribute{
Description: "The name of the Kubernetes cluster that you are currently deploying infrastructure to",
MarkdownDescription: "The name of the Kubernetes cluster that you are currently deploying infrastructure to",
Computed: true,
},
},
}
}

func (d *metadataDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

data, ok := req.ProviderData.(*PanfactumProvider)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected PanfactumProviderModel, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.ProviderData = data
}

func (d *metadataDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {

var data metadataDataSourceModel

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

data.Environment = d.ProviderData.Environment
data.Region = d.ProviderData.Region
data.RootModule = d.ProviderData.RootModule
data.StackCommit = d.ProviderData.StackCommit
data.StackVersion = d.ProviderData.StackVersion
data.IsLocal = d.ProviderData.IsLocal
data.KubeConfigPath = types.StringValue(d.ProviderData.KubeConfigPath)
data.KubeConfigContext = d.ProviderData.KubeConfigContext
data.KubeAPIServer = d.ProviderData.KubeAPIServer
data.KubeClusterName = d.ProviderData.KubeClusterName

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
Loading

0 comments on commit 95086ad

Please sign in to comment.