in internal/provider/resource_gitlab_group_protected_environment.go [291:400]
func (r *gitlabGroupProtectedEnvironmentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *gitlabGroupProtectedEnvironmentResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
// local copies of plan arguments
groupID := data.Group.ValueString()
environmentName := data.Environment.ValueString()
// configure GitLab API call
options := &gitlab.ProtectGroupEnvironmentOptions{
Name: gitlab.Ptr(environmentName),
}
// deploy access levels
deployAccessLevels := make([]*gitlabGroupProtectedEnvironmentDeployAccessLevelModel, 0, len(data.DeployAccessLevels.Elements()))
resp.Diagnostics.Append(data.DeployAccessLevels.ElementsAs(ctx, &deployAccessLevels, false)...)
if resp.Diagnostics.HasError() {
return
}
deployAccessLevelsOption := make([]*gitlab.GroupEnvironmentAccessOptions, len(deployAccessLevels))
for i, v := range deployAccessLevels {
deployAccessLevelOptions := &gitlab.GroupEnvironmentAccessOptions{}
if !v.AccessLevel.IsNull() && v.AccessLevel.ValueString() != "" {
deployAccessLevelOptions.AccessLevel = gitlab.Ptr(api.AccessLevelNameToValue[v.AccessLevel.ValueString()])
}
if !v.UserId.IsNull() && v.UserId.ValueInt64() != 0 {
deployAccessLevelOptions.UserID = gitlab.Ptr(int(v.UserId.ValueInt64()))
}
if !v.GroupId.IsNull() && v.GroupId.ValueInt64() != 0 {
deployAccessLevelOptions.GroupID = gitlab.Ptr(int(v.GroupId.ValueInt64()))
}
if !v.GroupInheritanceType.IsNull() && v.GroupInheritanceType.ValueInt64() != 0 {
deployAccessLevelOptions.GroupInheritanceType = gitlab.Ptr(int(v.GroupInheritanceType.ValueInt64()))
}
deployAccessLevelsOption[i] = deployAccessLevelOptions
}
options.DeployAccessLevels = &deployAccessLevelsOption
// approval rules
approvalRules := make([]*gitlabGroupProtectedEnvironmentApprovalRuleModel, 0, len(data.ApprovalRules.Elements()))
resp.Diagnostics.Append(data.ApprovalRules.ElementsAs(ctx, &approvalRules, true)...)
if resp.Diagnostics.HasError() {
return
}
approvalRulesOption := make([]*gitlab.GroupEnvironmentApprovalRuleOptions, len(approvalRules))
for i, v := range approvalRules {
approvalRuleOptions := &gitlab.GroupEnvironmentApprovalRuleOptions{}
if !v.AccessLevel.IsNull() && v.AccessLevel.ValueString() != "" {
approvalRuleOptions.AccessLevel = gitlab.Ptr(api.AccessLevelNameToValue[v.AccessLevel.ValueString()])
}
if !v.UserId.IsNull() && v.UserId.ValueInt64() != 0 {
approvalRuleOptions.UserID = gitlab.Ptr(int(v.UserId.ValueInt64()))
}
if !v.GroupId.IsNull() && v.GroupId.ValueInt64() != 0 {
approvalRuleOptions.GroupID = gitlab.Ptr(int(v.GroupId.ValueInt64()))
}
if !v.GroupInheritanceType.IsNull() && v.GroupInheritanceType.ValueInt64() != 0 {
approvalRuleOptions.GroupInheritanceType = gitlab.Ptr(int(v.GroupInheritanceType.ValueInt64()))
}
if !v.RequiredApprovals.IsNull() && v.RequiredApprovals.ValueInt64() != 0 {
approvalRuleOptions.RequiredApprovalCount = gitlab.Ptr(int(v.RequiredApprovals.ValueInt64()))
}
approvalRulesOption[i] = approvalRuleOptions
}
options.ApprovalRules = &approvalRulesOption
tflog.Debug(ctx, "Creating group protected environment with options", map[string]any{
"data": data,
"groupId": groupID,
"name": environmentName,
"options": options,
})
// Protect environment
protectedEnvironment, _, err := r.client.GroupProtectedEnvironments.ProtectGroupEnvironment(groupID, options, gitlab.WithContext(ctx))
if err != nil {
if api.Is404(err) {
resp.Diagnostics.AddError(
"GitLab Feature not available",
fmt.Sprintf("The protected environment 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 protect environment: %s", err.Error()))
return
}
tflog.Debug(ctx, "Group Protected Environment before state is persisted", map[string]any{
"data": data,
"groupId": groupID,
"name": environmentName,
"environment": protectedEnvironment,
})
// Create resource ID and persist in state model
data.Id = types.StringValue(utils.BuildTwoPartID(&groupID, &protectedEnvironment.Name))
// persist API response in state model
r.protectedEnvironmentToStateModel(ctx, resp.Diagnostics, groupID, protectedEnvironment, data)
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}