|
| 1 | +package octopusdeploy_framework |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/deploymentfreezes" |
| 6 | + "github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/schemas" |
| 7 | + "github.com/OctopusDeploy/terraform-provider-octopusdeploy/octopusdeploy_framework/util" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +const deploymentFreezeDatasourceName = "deployment_freezes" |
| 16 | + |
| 17 | +type deploymentFreezesModel struct { |
| 18 | + ID types.String `tfsdk:"id"` |
| 19 | + IDs types.List `tfsdk:"ids"` |
| 20 | + PartialName types.String `tfsdk:"partial_name"` |
| 21 | + ProjectIDs types.List `tfsdk:"project_ids"` |
| 22 | + EnvironmentIDs types.List `tfsdk:"environment_ids"` |
| 23 | + IncludeComplete types.Bool `tfsdk:"include_complete"` |
| 24 | + Status types.String `tfsdk:"status"` |
| 25 | + Skip types.Int64 `tfsdk:"skip"` |
| 26 | + Take types.Int64 `tfsdk:"take"` |
| 27 | + DeploymentFreezes types.List `tfsdk:"deployment_freezes"` |
| 28 | +} |
| 29 | + |
| 30 | +type deploymentFreezeDataSource struct { |
| 31 | + *Config |
| 32 | +} |
| 33 | + |
| 34 | +func (d *deploymentFreezeDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 35 | + d.Config = DataSourceConfiguration(req, resp) |
| 36 | +} |
| 37 | + |
| 38 | +func NewDeploymentFreezeDataSource() datasource.DataSource { |
| 39 | + return &deploymentFreezeDataSource{} |
| 40 | +} |
| 41 | + |
| 42 | +func (d *deploymentFreezeDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 43 | + resp.TypeName = util.GetTypeName(deploymentFreezeDatasourceName) |
| 44 | +} |
| 45 | + |
| 46 | +func (d *deploymentFreezeDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 47 | + resp.Schema = schemas.DeploymentFreezeSchema{}.GetDatasourceSchema() |
| 48 | +} |
| 49 | + |
| 50 | +func (d *deploymentFreezeDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 51 | + var data deploymentFreezesModel |
| 52 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 53 | + if resp.Diagnostics.HasError() { |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + query := deploymentfreezes.DeploymentFreezeQuery{ |
| 58 | + IDs: util.Ternary(data.IDs.IsNull(), []string{}, util.ExpandStringList(data.IDs)), |
| 59 | + PartialName: data.PartialName.ValueString(), |
| 60 | + ProjectIds: util.Ternary(data.ProjectIDs.IsNull(), []string{}, util.ExpandStringList(data.ProjectIDs)), |
| 61 | + EnvironmentIds: util.Ternary(data.EnvironmentIDs.IsNull(), []string{}, util.ExpandStringList(data.EnvironmentIDs)), |
| 62 | + IncludeComplete: data.IncludeComplete.ValueBool(), |
| 63 | + Status: data.Status.ValueString(), |
| 64 | + Skip: int(data.Skip.ValueInt64()), |
| 65 | + Take: int(data.Take.ValueInt64()), |
| 66 | + } |
| 67 | + |
| 68 | + util.DatasourceReading(ctx, "deployment freezes", query) |
| 69 | + |
| 70 | + existingFreezes, err := deploymentfreezes.Get(d.Client, query) |
| 71 | + if err != nil { |
| 72 | + resp.Diagnostics.AddError("unable to load deployment freezes", err.Error()) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + flattenedFreezes := []interface{}{} |
| 77 | + for _, freeze := range existingFreezes.DeploymentFreezes { |
| 78 | + flattenedFreeze, diags := mapFreezeToAttribute(ctx, freeze) |
| 79 | + if diags.HasError() { |
| 80 | + resp.Diagnostics.Append(diags...) |
| 81 | + return |
| 82 | + } |
| 83 | + flattenedFreezes = append(flattenedFreezes, flattenedFreeze) |
| 84 | + } |
| 85 | + |
| 86 | + data.ID = types.StringValue("Deployment Freezes " + time.Now().UTC().String()) |
| 87 | + data.DeploymentFreezes, _ = types.ListValueFrom(ctx, types.ObjectType{AttrTypes: freezeObjectType()}, flattenedFreezes) |
| 88 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 89 | +} |
| 90 | + |
| 91 | +var _ datasource.DataSource = &deploymentFreezeDataSource{} |
| 92 | +var _ datasource.DataSourceWithConfigure = &deploymentFreezeDataSource{} |
| 93 | + |
| 94 | +func mapFreezeToAttribute(ctx context.Context, freeze deploymentfreezes.DeploymentFreeze) (attr.Value, diag.Diagnostics) { |
| 95 | + projectScopes := make(map[string]attr.Value) |
| 96 | + for projectId, environmentScopes := range freeze.ProjectEnvironmentScope { |
| 97 | + projectScopes[projectId] = util.FlattenStringList(environmentScopes) |
| 98 | + } |
| 99 | + |
| 100 | + scopeType, diags := types.MapValueFrom(ctx, types.ListType{ElemType: types.StringType}, projectScopes) |
| 101 | + if diags.HasError() { |
| 102 | + return nil, diags |
| 103 | + } |
| 104 | + |
| 105 | + return types.ObjectValueMust(freezeObjectType(), map[string]attr.Value{ |
| 106 | + "id": types.StringValue(freeze.ID), |
| 107 | + "name": types.StringValue(freeze.Name), |
| 108 | + "start": types.StringValue(freeze.Start.Format(time.RFC3339)), |
| 109 | + "end": types.StringValue(freeze.End.Format(time.RFC3339)), |
| 110 | + "project_environment_scope": scopeType, |
| 111 | + }), diags |
| 112 | +} |
| 113 | + |
| 114 | +func freezeObjectType() map[string]attr.Type { |
| 115 | + return map[string]attr.Type{ |
| 116 | + "id": types.StringType, |
| 117 | + "name": types.StringType, |
| 118 | + "start": types.StringType, |
| 119 | + "end": types.StringType, |
| 120 | + "project_environment_scope": types.MapType{ElemType: types.ListType{ElemType: types.StringType}}, |
| 121 | + } |
| 122 | +} |
0 commit comments