|
| 1 | +// Copyright (c) 2025 - Stacklet, Inc. |
| 2 | + |
| 3 | +package datasources |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | + |
| 12 | + "github.com/stacklet/terraform-provider-stacklet/internal/api" |
| 13 | + "github.com/stacklet/terraform-provider-stacklet/internal/errors" |
| 14 | + "github.com/stacklet/terraform-provider-stacklet/internal/models" |
| 15 | + "github.com/stacklet/terraform-provider-stacklet/internal/modelupdate" |
| 16 | + "github.com/stacklet/terraform-provider-stacklet/internal/providerdata" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + _ datasource.DataSource = &configurationProfileJiraDataSource{} |
| 21 | +) |
| 22 | + |
| 23 | +func NewConfigurationProfileJiraDataSource() datasource.DataSource { |
| 24 | + return &configurationProfileJiraDataSource{} |
| 25 | +} |
| 26 | + |
| 27 | +type configurationProfileJiraDataSource struct { |
| 28 | + api *api.API |
| 29 | +} |
| 30 | + |
| 31 | +func (d *configurationProfileJiraDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 32 | + resp.TypeName = req.ProviderTypeName + "_configuration_profile_jira" |
| 33 | +} |
| 34 | + |
| 35 | +func (d *configurationProfileJiraDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 36 | + resp.Schema = schema.Schema{ |
| 37 | + Description: "Retrieve information about the Jira configuration profile.", |
| 38 | + Attributes: map[string]schema.Attribute{ |
| 39 | + "id": schema.StringAttribute{ |
| 40 | + Description: "The GraphQL Node ID of the configuration profile.", |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "profile": schema.StringAttribute{ |
| 44 | + Description: "The profile name.", |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "url": schema.StringAttribute{ |
| 48 | + Description: "The Jira instance URL.", |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "user": schema.StringAttribute{ |
| 52 | + Description: "The Jira instance authentication username.", |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + }, |
| 56 | + Blocks: map[string]schema.Block{ |
| 57 | + "project": schema.ListNestedBlock{ |
| 58 | + Description: "Jira project configuration.", |
| 59 | + NestedObject: schema.NestedBlockObject{ |
| 60 | + Attributes: map[string]schema.Attribute{ |
| 61 | + "closed_status": schema.StringAttribute{ |
| 62 | + Description: "The state for closed tickets.", |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + "issue_type": schema.StringAttribute{ |
| 66 | + Description: "The type of issue to use for tickets.", |
| 67 | + Computed: true, |
| 68 | + }, |
| 69 | + "name": schema.StringAttribute{ |
| 70 | + Description: "The name of the project.", |
| 71 | + Computed: true, |
| 72 | + }, |
| 73 | + "project": schema.StringAttribute{ |
| 74 | + Description: "The ID of the project.", |
| 75 | + Computed: true, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func (d *configurationProfileJiraDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 85 | + if pd, err := providerdata.GetDataSourceProviderData(req); err != nil { |
| 86 | + errors.AddDiagError(&resp.Diagnostics, err) |
| 87 | + } else if pd != nil { |
| 88 | + d.api = pd.API |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func (d *configurationProfileJiraDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 93 | + var data models.ConfigurationProfileJiraDataSource |
| 94 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 95 | + if resp.Diagnostics.HasError() { |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + config, err := d.api.ConfigurationProfile.ReadJira(ctx) |
| 100 | + if err != nil { |
| 101 | + errors.AddDiagError(&resp.Diagnostics, err) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + data.ID = types.StringValue(config.ID) |
| 106 | + data.Profile = types.StringValue(config.Profile) |
| 107 | + data.URL = types.StringPointerValue(config.Record.JiraConfiguration.URL) |
| 108 | + data.User = types.StringValue(config.Record.JiraConfiguration.User) |
| 109 | + |
| 110 | + updater := modelupdate.NewConfigurationProfileUpdater(*config) |
| 111 | + projects, diags := updater.JiraProjects() |
| 112 | + resp.Diagnostics.Append(diags...) |
| 113 | + if resp.Diagnostics.HasError() { |
| 114 | + return |
| 115 | + } |
| 116 | + data.Projects = projects |
| 117 | + |
| 118 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 119 | +} |
0 commit comments