in internal/provider/resource_gitlab_group_issue_board.go [381:518]
func (r *gitlabGroupIssueBoardResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *gitlabGroupIssueBoardResourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// local copies of plan arguments
// read all information for refresh from resource id
groupID := data.Group.ValueString()
boardName := data.Name.ValueString()
// configure GitLab API call
options := &gitlab.CreateGroupIssueBoardOptions{
Name: gitlab.Ptr(boardName),
}
optionsUpdate := &gitlab.UpdateGroupIssueBoardOptions{
Name: gitlab.Ptr(boardName),
}
if !data.MilestoneId.IsNull() && !data.MilestoneId.IsUnknown() {
optionsUpdate.MilestoneID = gitlab.Ptr(int(data.MilestoneId.ValueInt64()))
}
if !data.Labels.IsNull() && !data.Labels.IsUnknown() {
// convert the Set to a []string and pass it in
var labels []string
data.Labels.ElementsAs(ctx, &labels, true)
gitlabLabels := gitlab.LabelOptions(labels)
optionsUpdate.Labels = &gitlabLabels
}
issueBoard, _, err := r.client.GroupIssueBoards.CreateGroupIssueBoard(groupID, options, gitlab.WithContext(ctx))
if err != nil {
if api.Is404(err) {
resp.Diagnostics.AddError(
"GitLab Feature not available",
fmt.Sprintf("The group issue board feature is not available on this group. Make sure it's part of an enterprise plan. Error: %s", err.Error()),
)
}
// If we get here and we have a hydrated issue board, save the board to state.
// something... really weird happened.
if issueBoard != nil {
tflog.Warn(ctx, "Creating Group Issue Board encountered an error, but still returned a hydrated issue board. Aborting.", map[string]any{
"group": groupID,
"board": issueBoard.Name,
})
// persist API response in state model
data.Id = types.StringValue(fmt.Sprintf("%s:%d", groupID, issueBoard.ID))
r.groupIssueBoardToStateModel(ctx, groupID, issueBoard, data)
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
resp.Diagnostics.AddError("GitLab API error occurred", fmt.Sprintf("Unable to create issue board: %s", err.Error()))
return
}
issueBoard, _, err = r.client.GroupIssueBoards.UpdateIssueBoard(groupID, issueBoard.ID, optionsUpdate, gitlab.WithContext(ctx))
if err != nil {
if api.Is404(err) {
resp.Diagnostics.AddError(
"GitLab Feature not available",
fmt.Sprintf("The group issue board feature is not available on this group. Make sure it's part of an enterprise plan. Error: %s", err.Error()),
)
}
// If we get here and we have a hydrated issue board, save the board to state before we error.
if issueBoard != nil {
tflog.Warn(ctx, "Creating Group Issue Board encountered an error, but still returned a hydrated issue board. Aborting.", map[string]any{
"group": groupID,
"board": issueBoard.Name,
})
// persist API response in state model
data.Id = types.StringValue(fmt.Sprintf("%s:%d", groupID, issueBoard.ID))
r.groupIssueBoardToStateModel(ctx, groupID, issueBoard, data)
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
resp.Diagnostics.AddError("GitLab API error occurred", fmt.Sprintf("Unable to create issue board: %s", err.Error()))
return
}
listsData := make([]*gitlab.BoardList, len(data.Lists))
// Sort data.Lists based on list Position
sort.Slice(data.Lists, func(i, j int) bool {
labelPosI := data.Lists[i].Position
labelPosJ := data.Lists[j].Position
// Handle nil values
if (labelPosI.IsNull() || labelPosI.IsUnknown()) && (labelPosJ.IsNull() || labelPosJ.IsUnknown()) {
return false // Treat two nils as equal
} else if labelPosI.IsNull() || labelPosI.IsUnknown() {
return true // Nil should come after non-nil
} else if labelPosJ.IsNull() || labelPosJ.IsUnknown() {
return false // Non-nil should come before nil
}
return labelPosI.ValueInt64() < labelPosJ.ValueInt64()
})
for i, v := range data.Lists {
listOptions := &gitlab.CreateGroupIssueBoardListOptions{}
listOptions.LabelID = gitlab.Ptr(int(v.LabelId.ValueInt64()))
issueBoardList, _, err := r.client.GroupIssueBoards.CreateGroupIssueBoardList(groupID, issueBoard.ID, listOptions, gitlab.WithContext(ctx))
if err != nil {
if api.Is404(err) {
resp.Diagnostics.AddError(
"GitLab Feature not available",
fmt.Sprintf("The group issue board feature is not available on this group. Make sure it's part of an enterprise plan. Error: %s", err.Error()),
)
return
}
resp.Diagnostics.AddError("GitLab API error occurred", fmt.Sprintf("Unable to create issue board: %s", err.Error()))
return
}
listsData[i] = issueBoardList
}
issueBoard.Lists = listsData
// persist API response in state model
boardID := fmt.Sprintf("%d", issueBoard.ID)
data.Id = types.StringValue(utils.BuildTwoPartID(&groupID, &boardID))
r.groupIssueBoardToStateModel(ctx, groupID, issueBoard, data)
// Log the creation of the resource
tflog.Debug(ctx, "created a group issue board", map[string]any{
"group": groupID, "board": issueBoard.Name,
})
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}