func()

in internal/provider/resource_gitlab_group_epic_board.go [417:563]


func (r *gitlabGroupEpicBoardResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
	var data *gitlabGroupEpicBoardResourceModel

	// 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()

	// we always resolve the group to gather the full path for subsequent GraphQL API calls which ALWAYS
	// require the full path ...
	group, _, err := r.client.Groups.GetGroup(groupID, nil, gitlab.WithContext(ctx))
	if err != nil {
		if api.Is404(err) {
			tflog.Debug(ctx, "group does not exist, removing resource from state", map[string]any{
				"group": groupID,
			})
			return
		}
		resp.Diagnostics.AddError("GitLab API error occurred", fmt.Sprintf("Unable to read group details: %s", err.Error()))
		return
	}

	labels := make([]Label, len(data.Lists))
	for i, v := range data.Lists {
		labels[i].ID = fmt.Sprintf("gid://gitlab/GroupLabel/%s", v.LabelId.String())
	}

	query := gitlab.GraphQLQuery{
		Query: fmt.Sprintf(`
			mutation {
				epicBoardCreate(
					input: {
						groupPath: "%s",
						name: "%s"
					}
				) {
					epicBoard {
						id,
						name,
						labels {
							nodes {
								id
							}
						}
					}
				}
			}`, group.FullPath, boardName),
	}
	tflog.Debug(ctx, "executing GraphQL Query to create epic board", map[string]any{
		"query": query.Query,
	})

	var response EpicBoardCreateResponse
	if _, err = r.client.GraphQL.Do(ctx, query, &response); err != nil {
		resp.Diagnostics.AddError("GitLab GraphQL error occurred", fmt.Sprintf("Unable to create epic board: %s from query %s", err.Error(), query.Query))
		return
	}
	if len(response.Errors) > 0 {
		allerr := fmt.Sprintf("From create response %v\n", response)
		for i, err := range response.Errors {
			allerr += fmt.Sprintf("Error %d Code: %s\n", i, err)
		}
		resp.Diagnostics.AddError("GitLab GraphQL error occurred", allerr)
		return
	}

	parts := strings.Split(response.Data.EpicBoardCreate.EpicBoard.Id, "/")
	epicBoardId := parts[len(parts)-1]
	EpicBoardId, err := strconv.Atoi(epicBoardId)
	if err != nil {
		resp.Diagnostics.AddError("GitLab API error occurred",
			fmt.Sprintf("Unable to create epic board: %s - ID %s, Name %s from resp %v with query %s",
				"EpicBoard ID not found in GraphQL response",
				response.Data.EpicBoardCreate.EpicBoard.Id,
				response.Data.EpicBoardCreate.EpicBoard.Name,
				response,
				query.Query))
		return
	}

	// Create the lists on the epic board
	boardID := fmt.Sprintf("%d", EpicBoardId)

	for _, v := range labels {
		query = gitlab.GraphQLQuery{
			Query: fmt.Sprintf(`mutation {
				epicBoardListCreate(
				  input: {
					boardId: "gid://gitlab/Boards::EpicBoard/%s",
					labelId: "%s"
				  }
				) {
				  list {
					id
  					position
					label {
						id
					}
				  }
				  errors
				}
			}`, boardID, v.ID),
		}
		var listResp map[string]any
		if _, err = r.client.GraphQL.Do(ctx, query, &listResp); err != nil {
			resp.Diagnostics.AddError("GitLab API error occurred", fmt.Sprintf("Unable to create epic board list: %s from query %s", err.Error(), query.Query))
		}
		if listResp["errors"] != nil {
			resp.Diagnostics.AddError("GitLab API error occurred", fmt.Sprintf("Unable to create epic board list: %v from query %s", listResp["errors"], query.Query))
		}
	}

	if resp.Diagnostics.HasError() {
		return
	}

	groupEpicBoard, _, err := r.client.GroupEpicBoards.GetGroupEpicBoard(group.ID, EpicBoardId, gitlab.WithContext(ctx))
	if err != nil {
		if api.Is404(err) {
			tflog.Debug(ctx, "group epic board does not exist, removing from state", map[string]any{
				"group": groupID, "environment": EpicBoardId,
			})
			resp.State.RemoveResource(ctx)
			return
		}
		resp.Diagnostics.AddError("GitLab API error occured", fmt.Sprintf("Unable to read gitlab group epic board details: %s", err.Error()))
		return
	}

	data.Id = types.StringValue(utils.BuildTwoPartID(&groupID, &boardID))
	r.groupEpicBoardToStateModel(groupID, groupEpicBoard, data)

	// Log the creation of the resource
	tflog.Debug(ctx, "created a group epic board", map[string]any{
		"group": groupID, "board": groupEpicBoard.Name,
	})

	// Save data into Terraform state
	resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}