services/google/monitoring/group_internal.go (592 lines of code) (raw):
// Copyright 2025 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package monitoring
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
)
func (r *Group) validate() error {
if err := dcl.Required(r, "displayName"); err != nil {
return err
}
if err := dcl.Required(r, "filter"); err != nil {
return err
}
if err := dcl.RequiredParameter(r.Project, "Project"); err != nil {
return err
}
return nil
}
func (r *Group) basePath() string {
params := map[string]interface{}{}
return dcl.Nprintf("https://monitoring.googleapis.com/v3/", params)
}
func (r *Group) getURL(userBasePath string) (string, error) {
nr := r.urlNormalized()
params := map[string]interface{}{
"project": dcl.ValueOrEmptyString(nr.Project),
"name": dcl.ValueOrEmptyString(nr.Name),
}
return dcl.URL("projects/{{project}}/groups/{{name}}", nr.basePath(), userBasePath, params), nil
}
func (r *Group) listURL(userBasePath string) (string, error) {
nr := r.urlNormalized()
params := map[string]interface{}{
"project": dcl.ValueOrEmptyString(nr.Project),
}
return dcl.URL("projects/{{project}}/groups", nr.basePath(), userBasePath, params), nil
}
func (r *Group) createURL(userBasePath string) (string, error) {
nr := r.urlNormalized()
params := map[string]interface{}{
"project": dcl.ValueOrEmptyString(nr.Project),
}
return dcl.URL("projects/{{project}}/groups", nr.basePath(), userBasePath, params), nil
}
func (r *Group) deleteURL(userBasePath string) (string, error) {
nr := r.urlNormalized()
params := map[string]interface{}{
"project": dcl.ValueOrEmptyString(nr.Project),
"name": dcl.ValueOrEmptyString(nr.Name),
}
return dcl.URL("projects/{{project}}/groups/{{name}}", nr.basePath(), userBasePath, params), nil
}
// groupApiOperation represents a mutable operation in the underlying REST
// API such as Create, Update, or Delete.
type groupApiOperation interface {
do(context.Context, *Group, *Client) error
}
// newUpdateGroupUpdateRequest creates a request for an
// Group resource's update update type by filling in the update
// fields based on the intended state of the resource.
func newUpdateGroupUpdateRequest(ctx context.Context, f *Group, c *Client) (map[string]interface{}, error) {
req := map[string]interface{}{}
res := f
_ = res
if v := f.DisplayName; !dcl.IsEmptyValueIndirect(v) {
req["displayName"] = v
}
if v := f.Filter; !dcl.IsEmptyValueIndirect(v) {
req["filter"] = v
}
if v := f.IsCluster; !dcl.IsEmptyValueIndirect(v) {
req["isCluster"] = v
}
if v, err := dcl.DeriveField("projects/%s/groups/%s", f.ParentName, dcl.SelfLinkToName(f.Project), dcl.SelfLinkToName(f.ParentName)); err != nil {
return nil, fmt.Errorf("error expanding ParentName into parentName: %w", err)
} else if v != nil {
req["parentName"] = v
}
return req, nil
}
// marshalUpdateGroupUpdateRequest converts the update into
// the final JSON request body.
func marshalUpdateGroupUpdateRequest(c *Client, m map[string]interface{}) ([]byte, error) {
return json.Marshal(m)
}
type updateGroupUpdateOperation struct {
// If the update operation has the REQUIRES_APPLY_OPTIONS trait, this will be populated.
// Usually it will be nil - this is to prevent us from accidentally depending on apply
// options, which should usually be unnecessary.
ApplyOptions []dcl.ApplyOption
FieldDiffs []*dcl.FieldDiff
}
// do creates a request and sends it to the appropriate URL. In most operations,
// do will transcribe a subset of the resource into a request object and send a
// PUT request to a single URL.
func (op *updateGroupUpdateOperation) do(ctx context.Context, r *Group, c *Client) error {
_, err := c.GetGroup(ctx, r)
if err != nil {
return err
}
u, err := r.updateURL(c.Config.BasePath, "update")
if err != nil {
return err
}
req, err := newUpdateGroupUpdateRequest(ctx, r, c)
if err != nil {
return err
}
c.Config.Logger.InfoWithContextf(ctx, "Created update: %#v", req)
body, err := marshalUpdateGroupUpdateRequest(c, req)
if err != nil {
return err
}
_, err = dcl.SendRequest(ctx, c.Config, "PUT", u, bytes.NewBuffer(body), c.Config.RetryProvider)
if err != nil {
return err
}
return nil
}
func (c *Client) listGroupRaw(ctx context.Context, r *Group, pageToken string, pageSize int32) ([]byte, error) {
u, err := r.urlNormalized().listURL(c.Config.BasePath)
if err != nil {
return nil, err
}
m := make(map[string]string)
if pageToken != "" {
m["pageToken"] = pageToken
}
if pageSize != GroupMaxPage {
m["pageSize"] = fmt.Sprintf("%v", pageSize)
}
u, err = dcl.AddQueryParams(u, m)
if err != nil {
return nil, err
}
resp, err := dcl.SendRequest(ctx, c.Config, "GET", u, &bytes.Buffer{}, c.Config.RetryProvider)
if err != nil {
return nil, err
}
defer resp.Response.Body.Close()
return ioutil.ReadAll(resp.Response.Body)
}
type listGroupOperation struct {
Group []map[string]interface{} `json:"group"`
Token string `json:"nextPageToken"`
}
func (c *Client) listGroup(ctx context.Context, r *Group, pageToken string, pageSize int32) ([]*Group, string, error) {
b, err := c.listGroupRaw(ctx, r, pageToken, pageSize)
if err != nil {
return nil, "", err
}
var m listGroupOperation
if err := json.Unmarshal(b, &m); err != nil {
return nil, "", err
}
var l []*Group
for _, v := range m.Group {
res, err := unmarshalMapGroup(v, c, r)
if err != nil {
return nil, m.Token, err
}
res.Project = r.Project
l = append(l, res)
}
return l, m.Token, nil
}
func (c *Client) deleteAllGroup(ctx context.Context, f func(*Group) bool, resources []*Group) error {
var errors []string
for _, res := range resources {
if f(res) {
// We do not want deleteAll to fail on a deletion or else it will stop deleting other resources.
err := c.DeleteGroup(ctx, res)
if err != nil {
errors = append(errors, err.Error())
}
}
}
if len(errors) > 0 {
return fmt.Errorf("%v", strings.Join(errors, "\n"))
} else {
return nil
}
}
type deleteGroupOperation struct{}
func (op *deleteGroupOperation) do(ctx context.Context, r *Group, c *Client) error {
r, err := c.GetGroup(ctx, r)
if err != nil {
if dcl.IsNotFound(err) {
c.Config.Logger.InfoWithContextf(ctx, "Group not found, returning. Original error: %v", err)
return nil
}
c.Config.Logger.WarningWithContextf(ctx, "GetGroup checking for existence. error: %v", err)
return err
}
u, err := r.deleteURL(c.Config.BasePath)
if err != nil {
return err
}
// Delete should never have a body
body := &bytes.Buffer{}
_, err = dcl.SendRequest(ctx, c.Config, "DELETE", u, body, c.Config.RetryProvider)
if err != nil {
return fmt.Errorf("failed to delete Group: %w", err)
}
// We saw a race condition where for some successful delete operation, the Get calls returned resources for a short duration.
// This is the reason we are adding retry to handle that case.
retriesRemaining := 10
dcl.Do(ctx, func(ctx context.Context) (*dcl.RetryDetails, error) {
_, err := c.GetGroup(ctx, r)
if dcl.IsNotFound(err) {
return nil, nil
}
if retriesRemaining > 0 {
retriesRemaining--
return &dcl.RetryDetails{}, dcl.OperationNotDone{}
}
return nil, dcl.NotDeletedError{ExistingResource: r}
}, c.Config.RetryProvider)
return nil
}
// Create operations are similar to Update operations, although they do not have
// specific request objects. The Create request object is the json encoding of
// the resource, which is modified by res.marshal to form the base request body.
type createGroupOperation struct {
response map[string]interface{}
}
func (op *createGroupOperation) FirstResponse() (map[string]interface{}, bool) {
return op.response, len(op.response) > 0
}
func (op *createGroupOperation) do(ctx context.Context, r *Group, c *Client) error {
c.Config.Logger.InfoWithContextf(ctx, "Attempting to create %v", r)
u, err := r.createURL(c.Config.BasePath)
if err != nil {
return err
}
req, err := r.marshal(c)
if err != nil {
return err
}
if r.Name != nil {
// Allowing creation to continue with Name set could result in a Group with the wrong Name.
return fmt.Errorf("server-generated parameter Name was specified by user as %v, should be unspecified", dcl.ValueOrEmptyString(r.Name))
}
resp, err := dcl.SendRequest(ctx, c.Config, "POST", u, bytes.NewBuffer(req), c.Config.RetryProvider)
if err != nil {
return err
}
o, err := dcl.ResponseBodyAsJSON(resp)
if err != nil {
return fmt.Errorf("error decoding response body into JSON: %w", err)
}
op.response = o
// Include Name in URL substitution for initial GET request.
m := op.response
r.Name = dcl.SelfLinkToName(dcl.FlattenString(m["name"]))
if _, err := c.GetGroup(ctx, r); err != nil {
c.Config.Logger.WarningWithContextf(ctx, "get returned error: %v", err)
return err
}
return nil
}
func (c *Client) getGroupRaw(ctx context.Context, r *Group) ([]byte, error) {
u, err := r.getURL(c.Config.BasePath)
if err != nil {
return nil, err
}
resp, err := dcl.SendRequest(ctx, c.Config, "GET", u, &bytes.Buffer{}, c.Config.RetryProvider)
if err != nil {
return nil, err
}
defer resp.Response.Body.Close()
b, err := ioutil.ReadAll(resp.Response.Body)
if err != nil {
return nil, err
}
return b, nil
}
func (c *Client) groupDiffsForRawDesired(ctx context.Context, rawDesired *Group, opts ...dcl.ApplyOption) (initial, desired *Group, diffs []*dcl.FieldDiff, err error) {
c.Config.Logger.InfoWithContext(ctx, "Fetching initial state...")
// First, let us see if the user provided a state hint. If they did, we will start fetching based on that.
var fetchState *Group
if sh := dcl.FetchStateHint(opts); sh != nil {
if r, ok := sh.(*Group); !ok {
c.Config.Logger.WarningWithContextf(ctx, "Initial state hint was of the wrong type; expected Group, got %T", sh)
} else {
fetchState = r
}
}
if fetchState == nil {
fetchState = rawDesired
}
if fetchState.Name == nil {
// We cannot perform a get because of lack of information. We have to assume
// that this is being created for the first time.
desired, err := canonicalizeGroupDesiredState(rawDesired, nil)
return nil, desired, nil, err
}
// 1.2: Retrieval of raw initial state from API
rawInitial, err := c.GetGroup(ctx, fetchState)
if rawInitial == nil {
if !dcl.IsNotFound(err) {
c.Config.Logger.WarningWithContextf(ctx, "Failed to retrieve whether a Group resource already exists: %s", err)
return nil, nil, nil, fmt.Errorf("failed to retrieve Group resource: %v", err)
}
c.Config.Logger.InfoWithContext(ctx, "Found that Group resource did not exist.")
// Perform canonicalization to pick up defaults.
desired, err = canonicalizeGroupDesiredState(rawDesired, rawInitial)
return nil, desired, nil, err
}
c.Config.Logger.InfoWithContextf(ctx, "Found initial state for Group: %v", rawInitial)
c.Config.Logger.InfoWithContextf(ctx, "Initial desired state for Group: %v", rawDesired)
// The Get call applies postReadExtract and so the result may contain fields that are not part of API version.
if err := extractGroupFields(rawInitial); err != nil {
return nil, nil, nil, err
}
// 1.3: Canonicalize raw initial state into initial state.
initial, err = canonicalizeGroupInitialState(rawInitial, rawDesired)
if err != nil {
return nil, nil, nil, err
}
c.Config.Logger.InfoWithContextf(ctx, "Canonicalized initial state for Group: %v", initial)
// 1.4: Canonicalize raw desired state into desired state.
desired, err = canonicalizeGroupDesiredState(rawDesired, rawInitial, opts...)
if err != nil {
return nil, nil, nil, err
}
c.Config.Logger.InfoWithContextf(ctx, "Canonicalized desired state for Group: %v", desired)
// 2.1: Comparison of initial and desired state.
diffs, err = diffGroup(c, desired, initial, opts...)
return initial, desired, diffs, err
}
func canonicalizeGroupInitialState(rawInitial, rawDesired *Group) (*Group, error) {
// TODO(magic-modules-eng): write canonicalizer once relevant traits are added.
return rawInitial, nil
}
/*
* Canonicalizers
*
* These are responsible for converting either a user-specified config or a
* GCP API response to a standard format that can be used for difference checking.
* */
func canonicalizeGroupDesiredState(rawDesired, rawInitial *Group, opts ...dcl.ApplyOption) (*Group, error) {
if rawInitial == nil {
// Since the initial state is empty, the desired state is all we have.
// We canonicalize the remaining nested objects with nil to pick up defaults.
return rawDesired, nil
}
canonicalDesired := &Group{}
if dcl.StringCanonicalize(rawDesired.DisplayName, rawInitial.DisplayName) {
canonicalDesired.DisplayName = rawInitial.DisplayName
} else {
canonicalDesired.DisplayName = rawDesired.DisplayName
}
if dcl.StringCanonicalize(rawDesired.Filter, rawInitial.Filter) {
canonicalDesired.Filter = rawInitial.Filter
} else {
canonicalDesired.Filter = rawDesired.Filter
}
if dcl.BoolCanonicalize(rawDesired.IsCluster, rawInitial.IsCluster) {
canonicalDesired.IsCluster = rawInitial.IsCluster
} else {
canonicalDesired.IsCluster = rawDesired.IsCluster
}
if dcl.IsZeroValue(rawDesired.Name) || (dcl.IsEmptyValueIndirect(rawDesired.Name) && dcl.IsEmptyValueIndirect(rawInitial.Name)) {
// Desired and initial values are equivalent, so set canonical desired value to initial value.
canonicalDesired.Name = rawInitial.Name
} else {
canonicalDesired.Name = rawDesired.Name
}
if dcl.PartialSelfLinkToSelfLink(rawDesired.ParentName, rawInitial.ParentName) {
canonicalDesired.ParentName = rawInitial.ParentName
} else {
canonicalDesired.ParentName = rawDesired.ParentName
}
if dcl.NameToSelfLink(rawDesired.Project, rawInitial.Project) {
canonicalDesired.Project = rawInitial.Project
} else {
canonicalDesired.Project = rawDesired.Project
}
return canonicalDesired, nil
}
func canonicalizeGroupNewState(c *Client, rawNew, rawDesired *Group) (*Group, error) {
if dcl.IsEmptyValueIndirect(rawNew.DisplayName) && dcl.IsEmptyValueIndirect(rawDesired.DisplayName) {
rawNew.DisplayName = rawDesired.DisplayName
} else {
if dcl.StringCanonicalize(rawDesired.DisplayName, rawNew.DisplayName) {
rawNew.DisplayName = rawDesired.DisplayName
}
}
if dcl.IsEmptyValueIndirect(rawNew.Filter) && dcl.IsEmptyValueIndirect(rawDesired.Filter) {
rawNew.Filter = rawDesired.Filter
} else {
if dcl.StringCanonicalize(rawDesired.Filter, rawNew.Filter) {
rawNew.Filter = rawDesired.Filter
}
}
if dcl.IsEmptyValueIndirect(rawNew.IsCluster) && dcl.IsEmptyValueIndirect(rawDesired.IsCluster) {
rawNew.IsCluster = rawDesired.IsCluster
} else {
if dcl.BoolCanonicalize(rawDesired.IsCluster, rawNew.IsCluster) {
rawNew.IsCluster = rawDesired.IsCluster
}
}
if dcl.IsEmptyValueIndirect(rawNew.Name) && dcl.IsEmptyValueIndirect(rawDesired.Name) {
rawNew.Name = rawDesired.Name
} else {
}
if dcl.IsEmptyValueIndirect(rawNew.ParentName) && dcl.IsEmptyValueIndirect(rawDesired.ParentName) {
rawNew.ParentName = rawDesired.ParentName
} else {
if dcl.PartialSelfLinkToSelfLink(rawDesired.ParentName, rawNew.ParentName) {
rawNew.ParentName = rawDesired.ParentName
}
}
rawNew.Project = rawDesired.Project
return rawNew, nil
}
// The differ returns a list of diffs, along with a list of operations that should be taken
// to remedy them. Right now, it does not attempt to consolidate operations - if several
// fields can be fixed with a patch update, it will perform the patch several times.
// Diffs on some fields will be ignored if the `desired` state has an empty (nil)
// value. This empty value indicates that the user does not care about the state for
// the field. Empty fields on the actual object will cause diffs.
// TODO(magic-modules-eng): for efficiency in some resources, add batching.
func diffGroup(c *Client, desired, actual *Group, opts ...dcl.ApplyOption) ([]*dcl.FieldDiff, error) {
if desired == nil || actual == nil {
return nil, fmt.Errorf("nil resource passed to diff - always a programming error: %#v, %#v", desired, actual)
}
c.Config.Logger.Infof("Diff function called with desired state: %v", desired)
c.Config.Logger.Infof("Diff function called with actual state: %v", actual)
var fn dcl.FieldName
var newDiffs []*dcl.FieldDiff
// New style diffs.
if ds, err := dcl.Diff(desired.DisplayName, actual.DisplayName, dcl.DiffInfo{OperationSelector: dcl.TriggersOperation("updateGroupUpdateOperation")}, fn.AddNest("DisplayName")); len(ds) != 0 || err != nil {
if err != nil {
return nil, err
}
newDiffs = append(newDiffs, ds...)
}
if ds, err := dcl.Diff(desired.Filter, actual.Filter, dcl.DiffInfo{OperationSelector: dcl.TriggersOperation("updateGroupUpdateOperation")}, fn.AddNest("Filter")); len(ds) != 0 || err != nil {
if err != nil {
return nil, err
}
newDiffs = append(newDiffs, ds...)
}
if ds, err := dcl.Diff(desired.IsCluster, actual.IsCluster, dcl.DiffInfo{OperationSelector: dcl.TriggersOperation("updateGroupUpdateOperation")}, fn.AddNest("IsCluster")); len(ds) != 0 || err != nil {
if err != nil {
return nil, err
}
newDiffs = append(newDiffs, ds...)
}
if ds, err := dcl.Diff(desired.Name, actual.Name, dcl.DiffInfo{Type: "ReferenceType", OperationSelector: dcl.RequiresRecreate()}, fn.AddNest("Name")); len(ds) != 0 || err != nil {
if err != nil {
return nil, err
}
newDiffs = append(newDiffs, ds...)
}
if ds, err := dcl.Diff(desired.ParentName, actual.ParentName, dcl.DiffInfo{Type: "ReferenceType", OperationSelector: dcl.TriggersOperation("updateGroupUpdateOperation")}, fn.AddNest("ParentName")); len(ds) != 0 || err != nil {
if err != nil {
return nil, err
}
newDiffs = append(newDiffs, ds...)
}
if ds, err := dcl.Diff(desired.Project, actual.Project, dcl.DiffInfo{Type: "ReferenceType", OperationSelector: dcl.RequiresRecreate()}, fn.AddNest("Project")); len(ds) != 0 || err != nil {
if err != nil {
return nil, err
}
newDiffs = append(newDiffs, ds...)
}
if len(newDiffs) > 0 {
c.Config.Logger.Infof("Diff function found diffs: %v", newDiffs)
}
return newDiffs, nil
}
// urlNormalized returns a copy of the resource struct with values normalized
// for URL substitutions. For instance, it converts long-form self-links to
// short-form so they can be substituted in.
func (r *Group) urlNormalized() *Group {
normalized := dcl.Copy(*r).(Group)
normalized.DisplayName = dcl.SelfLinkToName(r.DisplayName)
normalized.Filter = dcl.SelfLinkToName(r.Filter)
normalized.Name = dcl.SelfLinkToName(r.Name)
normalized.ParentName = dcl.SelfLinkToName(r.ParentName)
normalized.Project = dcl.SelfLinkToName(r.Project)
return &normalized
}
func (r *Group) updateURL(userBasePath, updateName string) (string, error) {
nr := r.urlNormalized()
if updateName == "update" {
fields := map[string]interface{}{
"project": dcl.ValueOrEmptyString(nr.Project),
"name": dcl.ValueOrEmptyString(nr.Name),
}
return dcl.URL("projects/{{project}}/groups/{{name}}", nr.basePath(), userBasePath, fields), nil
}
return "", fmt.Errorf("unknown update name: %s", updateName)
}
// marshal encodes the Group resource into JSON for a Create request, and
// performs transformations from the resource schema to the API schema if
// necessary.
func (r *Group) marshal(c *Client) ([]byte, error) {
m, err := expandGroup(c, r)
if err != nil {
return nil, fmt.Errorf("error marshalling Group: %w", err)
}
return json.Marshal(m)
}
// unmarshalGroup decodes JSON responses into the Group resource schema.
func unmarshalGroup(b []byte, c *Client, res *Group) (*Group, error) {
var m map[string]interface{}
if err := json.Unmarshal(b, &m); err != nil {
return nil, err
}
return unmarshalMapGroup(m, c, res)
}
func unmarshalMapGroup(m map[string]interface{}, c *Client, res *Group) (*Group, error) {
flattened := flattenGroup(c, m, res)
if flattened == nil {
return nil, fmt.Errorf("attempted to flatten empty json object")
}
return flattened, nil
}
// expandGroup expands Group into a JSON request object.
func expandGroup(c *Client, f *Group) (map[string]interface{}, error) {
m := make(map[string]interface{})
res := f
_ = res
if v := f.DisplayName; dcl.ValueShouldBeSent(v) {
m["displayName"] = v
}
if v := f.Filter; dcl.ValueShouldBeSent(v) {
m["filter"] = v
}
if v := f.IsCluster; dcl.ValueShouldBeSent(v) {
m["isCluster"] = v
}
if v := f.Name; dcl.ValueShouldBeSent(v) {
m["name"] = v
}
if v, err := dcl.DeriveField("projects/%s/groups/%s", f.ParentName, dcl.SelfLinkToName(f.Project), dcl.SelfLinkToName(f.ParentName)); err != nil {
return nil, fmt.Errorf("error expanding ParentName into parentName: %w", err)
} else if v != nil {
m["parentName"] = v
}
if v, err := dcl.EmptyValue(); err != nil {
return nil, fmt.Errorf("error expanding Project into project: %w", err)
} else if !dcl.IsEmptyValueIndirect(v) {
m["project"] = v
}
return m, nil
}
// flattenGroup flattens Group from a JSON request object into the
// Group type.
func flattenGroup(c *Client, i interface{}, res *Group) *Group {
m, ok := i.(map[string]interface{})
if !ok {
return nil
}
if len(m) == 0 {
return nil
}
resultRes := &Group{}
resultRes.DisplayName = dcl.FlattenString(m["displayName"])
resultRes.Filter = dcl.FlattenString(m["filter"])
resultRes.IsCluster = dcl.FlattenBool(m["isCluster"])
resultRes.Name = dcl.SelfLinkToName(dcl.FlattenString(m["name"]))
resultRes.ParentName = dcl.FlattenString(m["parentName"])
resultRes.Project = dcl.FlattenString(m["project"])
return resultRes
}
// This function returns a matcher that checks whether a serialized resource matches this resource
// in its parameters (as defined by the fields in a Get, which definitionally define resource
// identity). This is useful in extracting the element from a List call.
func (r *Group) matcher(c *Client) func([]byte) bool {
return func(b []byte) bool {
cr, err := unmarshalGroup(b, c, r)
if err != nil {
c.Config.Logger.Warning("failed to unmarshal provided resource in matcher.")
return false
}
nr := r.urlNormalized()
ncr := cr.urlNormalized()
c.Config.Logger.Infof("looking for %v\nin %v", nr, ncr)
if nr.Project == nil && ncr.Project == nil {
c.Config.Logger.Info("Both Project fields null - considering equal.")
} else if nr.Project == nil || ncr.Project == nil {
c.Config.Logger.Info("Only one Project field is null - considering unequal.")
return false
} else if *nr.Project != *ncr.Project {
return false
}
if nr.Name == nil && ncr.Name == nil {
c.Config.Logger.Info("Both Name fields null - considering equal.")
} else if nr.Name == nil || ncr.Name == nil {
c.Config.Logger.Info("Only one Name field is null - considering unequal.")
return false
} else if *nr.Name != *ncr.Name {
return false
}
return true
}
}
type groupDiff struct {
// The diff should include one or the other of RequiresRecreate or UpdateOp.
RequiresRecreate bool
UpdateOp groupApiOperation
FieldName string // used for error logging
}
func convertFieldDiffsToGroupDiffs(config *dcl.Config, fds []*dcl.FieldDiff, opts []dcl.ApplyOption) ([]groupDiff, error) {
opNamesToFieldDiffs := make(map[string][]*dcl.FieldDiff)
// Map each operation name to the field diffs associated with it.
for _, fd := range fds {
for _, ro := range fd.ResultingOperation {
if fieldDiffs, ok := opNamesToFieldDiffs[ro]; ok {
fieldDiffs = append(fieldDiffs, fd)
opNamesToFieldDiffs[ro] = fieldDiffs
} else {
config.Logger.Infof("%s required due to diff: %v", ro, fd)
opNamesToFieldDiffs[ro] = []*dcl.FieldDiff{fd}
}
}
}
var diffs []groupDiff
// For each operation name, create a groupDiff which contains the operation.
for opName, fieldDiffs := range opNamesToFieldDiffs {
// Use the first field diff's field name for logging required recreate error.
diff := groupDiff{FieldName: fieldDiffs[0].FieldName}
if opName == "Recreate" {
diff.RequiresRecreate = true
} else {
apiOp, err := convertOpNameToGroupApiOperation(opName, fieldDiffs, opts...)
if err != nil {
return diffs, err
}
diff.UpdateOp = apiOp
}
diffs = append(diffs, diff)
}
return diffs, nil
}
func convertOpNameToGroupApiOperation(opName string, fieldDiffs []*dcl.FieldDiff, opts ...dcl.ApplyOption) (groupApiOperation, error) {
switch opName {
case "updateGroupUpdateOperation":
return &updateGroupUpdateOperation{FieldDiffs: fieldDiffs}, nil
default:
return nil, fmt.Errorf("no such operation with name: %v", opName)
}
}
func extractGroupFields(r *Group) error {
return nil
}
func postReadExtractGroupFields(r *Group) error {
return nil
}