-
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
feat: jaas group data source #602
Merged
Merged
Changes from all commits
Commits
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,40 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "juju_jaas_group Data Source - terraform-provider-juju" | ||
subcategory: "" | ||
description: |- | ||
A data source representing a Juju JAAS Group. | ||
--- | ||
|
||
# juju_jaas_group (Data Source) | ||
|
||
A data source representing a Juju JAAS Group. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "juju_jaas_group" "test" { | ||
name = "group-0" | ||
} | ||
|
||
data "juju_jaas_group" "test" { | ||
name = juju_jaas_group.test.name | ||
// from a separate plan use a string literal | ||
// name = "group-0" | ||
} | ||
|
||
output "group_uuid" { | ||
value = data.juju_jaas_group.test.uuid | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the group. | ||
|
||
### Read-Only | ||
|
||
- `uuid` (String) The UUID of the group. |
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,13 @@ | ||
resource "juju_jaas_group" "test" { | ||
name = "group-0" | ||
} | ||
|
||
data "juju_jaas_group" "test" { | ||
name = juju_jaas_group.test.name | ||
// from a separate plan use a string literal | ||
// name = "group-0" | ||
} | ||
|
||
output "group_uuid" { | ||
value = data.juju_jaas_group.test.uuid | ||
} |
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
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,115 @@ | ||
// 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" | ||
) | ||
|
||
type jaasGroupDataSource struct { | ||
client *juju.Client | ||
|
||
// subCtx is the context created with the new tflog subsystem for applications. | ||
subCtx context.Context | ||
} | ||
|
||
// NewJAASGroupDataSource returns a new JAAS group data source instance. | ||
func NewJAASGroupDataSource() datasource.DataSource { | ||
return &jaasGroupDataSource{} | ||
} | ||
|
||
type jaasGroupDataSourceModel struct { | ||
Name types.String `tfsdk:"name"` | ||
UUID types.String `tfsdk:"uuid"` | ||
} | ||
|
||
// Metadata returns the metadata for the JAAS group data source. | ||
func (d *jaasGroupDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_jaas_group" | ||
} | ||
|
||
// Schema defines the schema for JAAS groups. | ||
func (d *jaasGroupDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Description: "A data source representing a Juju JAAS Group.", | ||
Attributes: map[string]schema.Attribute{ | ||
"name": schema.StringAttribute{ | ||
Description: "The name of the group.", | ||
Required: true, | ||
}, | ||
"uuid": schema.StringAttribute{ | ||
Description: "The UUID of the group.", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Configure sets up the JAAS group data source with the provider data. | ||
func (d *jaasGroupDataSource) 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 *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
return | ||
} | ||
|
||
d.client = client | ||
d.subCtx = tflog.NewSubsystem(ctx, LogDataSourceJAASGroup) | ||
} | ||
|
||
// Read updates the group data source with the latest data from JAAS. | ||
func (d *jaasGroupDataSource) 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, "jaas-group") | ||
return | ||
} | ||
|
||
var data jaasGroupDataSourceModel | ||
|
||
// Read Terraform configuration state into the model | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Update the group with the latest data from JAAS | ||
group, err := d.client.Jaas.ReadGroupByName(data.Name.String()) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read group, got error: %v", err)) | ||
return | ||
} | ||
data.UUID = types.StringValue(group.UUID) | ||
d.trace(fmt.Sprintf("read group %q data source", data.Name)) | ||
|
||
// Save the updated group back to the state | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (d *jaasGroupDataSource) trace(msg string, additionalFields ...map[string]interface{}) { | ||
if d.subCtx == nil { | ||
return | ||
} | ||
|
||
//SubsystemTrace(subCtx, "datasource-jaas-group", "hello, world", map[string]interface{}{"foo": 123}) | ||
// Output: | ||
// {"@level":"trace","@message":"hello, world","@module":"juju.datasource-jaas-group","foo":123} | ||
tflog.SubsystemTrace(d.subCtx, LogDataSourceJAASGroup, msg, additionalFields...) | ||
} |
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,49 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the Apache License, Version 2.0, see LICENCE file for details. | ||
|
||
package provider | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
|
||
internaltesting "github.com/juju/terraform-provider-juju/internal/testing" | ||
) | ||
|
||
func TestAcc_DataSourceJAASGroup(t *testing.T) { | ||
OnlyTestAgainstJAAS(t) | ||
groupName := acctest.RandomWithPrefix("tf-jaas-group") | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: frameworkProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceJAASGroup(groupName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.juju_jaas_group.test", "name", groupName), | ||
resource.TestCheckResourceAttrSet("data.juju_jaas_group.test", "uuid"), | ||
resource.TestCheckResourceAttrPair("juju_jaas_group.test", "uuid", "data.juju_jaas_group.test", "uuid"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceJAASGroup(name string) string { | ||
return internaltesting.GetStringFromTemplateWithData( | ||
"testAccDataSourceJAASGroup", | ||
` | ||
resource "juju_jaas_group" "test" { | ||
name = "{{ .Name }}" | ||
} | ||
|
||
data "juju_jaas_group" "test" { | ||
name = juju_jaas_group.test.name | ||
} | ||
`, internaltesting.TemplateData{ | ||
"Name": name, | ||
}) | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
todo: add newline above. This group of imports should be in 3 stanzas.