func()

in teamcity/group.go [196:284]


func (r *groupResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
	var plan groupResourceModel
	diags := req.Plan.Get(ctx, &plan)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}

	var oldState groupResourceModel
	diags = req.State.Get(ctx, &oldState)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}

	// items present in old state but missing in a plan -> remove
	for _, i := range oldState.Roles {
		if !contains3(plan.Roles, i) {
			err := r.client.RemoveGroupRole(plan.Id.ValueString(), i.Id.ValueString(), scope(i))
			if err != nil {
				resp.Diagnostics.AddError(
					"Error removing group role",
					err.Error(),
				)
				return
			}
		}
	}

	// items missing in old state but present in a plan -> add
	for _, i := range plan.Roles {
		if !contains3(oldState.Roles, i) {
			err := r.client.AddGroupRole(plan.Id.ValueString(), i.Id.ValueString(), scope(i))
			if err != nil {
				resp.Diagnostics.AddError(
					"Error adding group role",
					err.Error(),
				)
				return
			}
		}
	}

	if !plan.ParentGroups.Equal(oldState.ParentGroups) {
		var parents []string
		diags = plan.ParentGroups.ElementsAs(ctx, &parents, false)
		resp.Diagnostics.Append(diags...)
		if resp.Diagnostics.HasError() {
			return
		}

		err := r.client.SetGroupParents(plan.Id.ValueString(), parents)
		if err != nil {
			resp.Diagnostics.AddError(
				"Error Reading group",
				err.Error(),
			)
			return
		}

	}

	actual, err := r.client.GetGroup(plan.Id.ValueString())
	if err != nil {
		resp.Diagnostics.AddError(
			"Error Reading group",
			err.Error(),
		)
		return
	}

	if actual == nil {
		resp.State.RemoveResource(ctx)
		return
	}

	newState := r.readState(actual)

	// Only set the key if it was explicitly set before
	if oldState.Key.IsNull() {
		newState.Key = types.StringNull()
	}

	diags = resp.State.Set(ctx, newState)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}
}