-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Data source juju_application #630
Open
shipperizer
wants to merge
4
commits into
juju:main
Choose a base branch
from
shipperizer:ds/application
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "juju_application Data Source - terraform-provider-juju" | ||
subcategory: "" | ||
description: |- | ||
A data source that represents a single Juju application deployment from a charm. | ||
--- | ||
|
||
# juju_application (Data Source) | ||
|
||
A data source that represents a single Juju application deployment from a charm. | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `model` (String) The name of the model where the application is deployed. | ||
- `name` (String) Name of the application deployment. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the Apache License, Version 2.0, see LICENCE file for details. | ||
|
||
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" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
|
||
"github.com/juju/terraform-provider-juju/internal/juju" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces. | ||
var _ datasource.DataSourceWithConfigure = &applicationDataSource{} | ||
|
||
// NewApplicationDataSource returns a new data source for a Juju application. | ||
func NewApplicationDataSource() datasource.DataSourceWithConfigure { | ||
shipperizer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &applicationDataSource{} | ||
} | ||
|
||
type applicationDataSource struct { | ||
client *juju.Client | ||
|
||
// context for the logging subsystem. | ||
subCtx context.Context | ||
} | ||
|
||
type applicationDataSourceModel struct { | ||
ApplicationName types.String `tfsdk:"name"` | ||
ModelName types.String `tfsdk:"model"` | ||
|
||
// ID required by the testing framework | ||
ID types.String `tfsdk:"id"` | ||
} | ||
|
||
// Metadata returns the full data source name as used in terraform plans. | ||
func (d *applicationDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_application" | ||
} | ||
|
||
// Schema returns the schema for the application data source. | ||
func (d *applicationDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "A data source that represents a single Juju application deployment from a charm.", | ||
Attributes: map[string]schema.Attribute{ | ||
"name": schema.StringAttribute{ | ||
Description: "Name of the application deployment.", | ||
Required: true, | ||
}, | ||
"model": schema.StringAttribute{ | ||
Description: "The name of the model where the application is deployed.", | ||
Required: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Configure enables provider-level data or clients to be set in the | ||
// provider-defined DataSource type. It is separately executed for each | ||
// ReadDataSource RPC. | ||
func (d *applicationDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
// Prevent panic if the provider has not been configured. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*juju.Client) | ||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Data Source Configure Type", | ||
fmt.Sprintf("Expected *juju.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
return | ||
} | ||
|
||
d.client = client | ||
d.subCtx = tflog.NewSubsystem(ctx, LogDataSourceApplication) | ||
} | ||
|
||
// Read is called when the provider must read resource values in order | ||
// to update state. Planned state values should be read from the | ||
// ReadRequest and new state values set on the ReadResponse. | ||
// Take the juju api input from the ID, it may not exist in the plan. | ||
// Only set optional values if they exist. | ||
func (d *applicationDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
// Prevent panic if the provider has not been configured. | ||
if d.client == nil { | ||
addDSClientNotConfiguredError(&resp.Diagnostics, "application") | ||
return | ||
} | ||
var data applicationDataSourceModel | ||
|
||
// Read Terraform prior data into the application | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
appName := data.ApplicationName.ValueString() | ||
modelName := data.ModelName.ValueString() | ||
d.trace("Read", map[string]interface{}{ | ||
"Model": modelName, | ||
"Name": appName, | ||
}) | ||
|
||
response, err := d.client.Applications.ReadApplication(&juju.ReadApplicationInput{ | ||
ModelName: modelName, | ||
AppName: appName, | ||
}) | ||
if err != nil { | ||
resp.Diagnostics.Append(handleApplicationNotFoundError(ctx, err, &resp.State)...) | ||
return | ||
} | ||
if response == nil { | ||
return | ||
} | ||
d.trace("read application", map[string]interface{}{"resource": appName, "response": response}) | ||
|
||
data.ApplicationName = types.StringValue(appName) | ||
data.ModelName = types.StringValue(modelName) | ||
|
||
d.trace("Found", applicationDataSourceModelForLogging(ctx, &data)) | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (d *applicationDataSource) trace(msg string, additionalFields ...map[string]interface{}) { | ||
if d.subCtx == nil { | ||
return | ||
} | ||
|
||
//SubsystemTrace(subCtx, "datasource-model", "hello, world", map[string]interface{}{"foo": 123}) | ||
// Output: | ||
// {"@level":"trace","@message":"hello, world","@module":"juju.datasource-model","foo":123} | ||
tflog.SubsystemTrace(d.subCtx, LogDataSourceApplication, msg, additionalFields...) | ||
} | ||
|
||
func applicationDataSourceModelForLogging(_ context.Context, app *applicationDataSourceModel) map[string]interface{} { | ||
value := map[string]interface{}{ | ||
"application-name": app.ApplicationName.ValueString(), | ||
"model": app.ModelName.ValueString(), | ||
} | ||
return value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the Apache License, Version 2.0, see LICENCE file for details. | ||
|
||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
) | ||
|
||
func TestAcc_DataSourceApplicationLXD_Edge(t *testing.T) { | ||
if testingCloud != LXDCloudTesting { | ||
t.Skip(t.Name() + " only runs with LXD") | ||
} | ||
modelName := acctest.RandomWithPrefix("tf-datasource-application-test-model") | ||
applicationName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: frameworkProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceApplicationLXD(modelName, applicationName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.juju_application.this", "model", modelName), | ||
resource.TestCheckResourceAttr("data.juju_application.this", "name", applicationName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAcc_DataSourceApplicationK8s_Edge(t *testing.T) { | ||
if testingCloud != MicroK8sTesting { | ||
t.Skip(t.Name() + " only runs with MicroK8s") | ||
} | ||
modelName := acctest.RandomWithPrefix("tf-datasource-application-test-model") | ||
applicationName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: frameworkProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceApplicationK8s(modelName, applicationName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.juju_application.this", "model", modelName), | ||
resource.TestCheckResourceAttr("data.juju_application.this", "name", applicationName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceApplicationLXD(modelName, applicationName string) string { | ||
return fmt.Sprintf(` | ||
resource "juju_model" "model" { | ||
name = %q | ||
} | ||
|
||
resource "juju_application" "this" { | ||
name = %q | ||
model = juju_model.model.name | ||
trust = true | ||
|
||
charm { | ||
name = "ubuntu" | ||
channel = "latest/stable" | ||
} | ||
} | ||
|
||
data "juju_application" "this" { | ||
model = juju_model.model.name | ||
name = juju_application.this.name | ||
|
||
}`, modelName, applicationName) | ||
} | ||
|
||
func testAccDataSourceApplicationK8s(modelName, applicationName string) string { | ||
return fmt.Sprintf(` | ||
resource "juju_model" "model" { | ||
name = %q | ||
} | ||
|
||
resource "juju_application" "this" { | ||
name = %q | ||
model = juju_model.model.name | ||
trust = true | ||
|
||
charm { | ||
name = "zinc-k8s" | ||
channel = "latest/stable" | ||
} | ||
} | ||
|
||
data "juju_application" "this" { | ||
model = juju_model.model.name | ||
name = juju_application.this.name | ||
|
||
}`, modelName, applicationName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe docs/index.md also needs to be adjusted to include the application data source.