services/google/cloudbuildv2/repository_internal.go (601 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 cloudbuildv2
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl/operations"
)
func (r *Repository) validate() error {
if err := dcl.Required(r, "name"); err != nil {
return err
}
if err := dcl.Required(r, "remoteUri"); err != nil {
return err
}
if err := dcl.RequiredParameter(r.Connection, "Connection"); err != nil {
return err
}
return nil
}
func (r *Repository) basePath() string {
params := map[string]interface{}{}
return dcl.Nprintf("https://cloudbuild.googleapis.com/v2/", params)
}
func (r *Repository) getURL(userBasePath string) (string, error) {
nr := r.urlNormalized()
params := map[string]interface{}{
"project": dcl.ValueOrEmptyString(nr.Project),
"location": dcl.ValueOrEmptyString(nr.Location),
"connection": dcl.ValueOrEmptyString(nr.Connection),
"name": dcl.ValueOrEmptyString(nr.Name),
}
return dcl.URL("projects/{{project}}/locations/{{location}}/connections/{{connection}}/repositories/{{name}}", nr.basePath(), userBasePath, params), nil
}
func (r *Repository) listURL(userBasePath string) (string, error) {
nr := r.urlNormalized()
params := map[string]interface{}{
"project": dcl.ValueOrEmptyString(nr.Project),
"location": dcl.ValueOrEmptyString(nr.Location),
"connection": dcl.ValueOrEmptyString(nr.Connection),
}
return dcl.URL("projects/{{project}}/locations/{{location}}/connections/{{connection}}/repositories", nr.basePath(), userBasePath, params), nil
}
func (r *Repository) createURL(userBasePath string) (string, error) {
nr := r.urlNormalized()
params := map[string]interface{}{
"project": dcl.ValueOrEmptyString(nr.Project),
"location": dcl.ValueOrEmptyString(nr.Location),
"connection": dcl.ValueOrEmptyString(nr.Connection),
"name": dcl.ValueOrEmptyString(nr.Name),
}
return dcl.URL("projects/{{project}}/locations/{{location}}/connections/{{connection}}/repositories?repositoryId={{name}}", nr.basePath(), userBasePath, params), nil
}
func (r *Repository) deleteURL(userBasePath string) (string, error) {
nr := r.urlNormalized()
params := map[string]interface{}{
"project": dcl.ValueOrEmptyString(nr.Project),
"location": dcl.ValueOrEmptyString(nr.Location),
"connection": dcl.ValueOrEmptyString(nr.Connection),
"name": dcl.ValueOrEmptyString(nr.Name),
}
return dcl.URL("projects/{{project}}/locations/{{location}}/connections/{{connection}}/repositories/{{name}}", nr.basePath(), userBasePath, params), nil
}
// repositoryApiOperation represents a mutable operation in the underlying REST
// API such as Create, Update, or Delete.
type repositoryApiOperation interface {
do(context.Context, *Repository, *Client) error
}
func (c *Client) listRepositoryRaw(ctx context.Context, r *Repository, 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 != RepositoryMaxPage {
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 listRepositoryOperation struct {
Repositories []map[string]interface{} `json:"repositories"`
Token string `json:"nextPageToken"`
}
func (c *Client) listRepository(ctx context.Context, r *Repository, pageToken string, pageSize int32) ([]*Repository, string, error) {
b, err := c.listRepositoryRaw(ctx, r, pageToken, pageSize)
if err != nil {
return nil, "", err
}
var m listRepositoryOperation
if err := json.Unmarshal(b, &m); err != nil {
return nil, "", err
}
var l []*Repository
for _, v := range m.Repositories {
res, err := unmarshalMapRepository(v, c, r)
if err != nil {
return nil, m.Token, err
}
res.Project = r.Project
res.Location = r.Location
res.Connection = r.Connection
l = append(l, res)
}
return l, m.Token, nil
}
func (c *Client) deleteAllRepository(ctx context.Context, f func(*Repository) bool, resources []*Repository) 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.DeleteRepository(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 deleteRepositoryOperation struct{}
func (op *deleteRepositoryOperation) do(ctx context.Context, r *Repository, c *Client) error {
r, err := c.GetRepository(ctx, r)
if err != nil {
if dcl.IsNotFound(err) {
c.Config.Logger.InfoWithContextf(ctx, "Repository not found, returning. Original error: %v", err)
return nil
}
c.Config.Logger.WarningWithContextf(ctx, "GetRepository 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{}
resp, err := dcl.SendRequest(ctx, c.Config, "DELETE", u, body, c.Config.RetryProvider)
if err != nil {
return err
}
// wait for object to be deleted.
var o operations.StandardGCPOperation
if err := dcl.ParseResponse(resp.Response, &o); err != nil {
return err
}
if err := o.Wait(context.WithValue(ctx, dcl.DoNotLogRequestsKey, true), c.Config, r.basePath(), "GET"); err != nil {
return 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.GetRepository(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 createRepositoryOperation struct {
response map[string]interface{}
}
func (op *createRepositoryOperation) FirstResponse() (map[string]interface{}, bool) {
return op.response, len(op.response) > 0
}
func (op *createRepositoryOperation) do(ctx context.Context, r *Repository, 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
}
resp, err := dcl.SendRequest(ctx, c.Config, "POST", u, bytes.NewBuffer(req), c.Config.RetryProvider)
if err != nil {
return err
}
// wait for object to be created.
var o operations.StandardGCPOperation
if err := dcl.ParseResponse(resp.Response, &o); err != nil {
return err
}
if err := o.Wait(context.WithValue(ctx, dcl.DoNotLogRequestsKey, true), c.Config, r.basePath(), "GET"); err != nil {
c.Config.Logger.Warningf("Creation failed after waiting for operation: %v", err)
return err
}
c.Config.Logger.InfoWithContextf(ctx, "Successfully waited for operation")
op.response, _ = o.FirstResponse()
if _, err := c.GetRepository(ctx, r); err != nil {
c.Config.Logger.WarningWithContextf(ctx, "get returned error: %v", err)
return err
}
return nil
}
func (c *Client) getRepositoryRaw(ctx context.Context, r *Repository) ([]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) repositoryDiffsForRawDesired(ctx context.Context, rawDesired *Repository, opts ...dcl.ApplyOption) (initial, desired *Repository, 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 *Repository
if sh := dcl.FetchStateHint(opts); sh != nil {
if r, ok := sh.(*Repository); !ok {
c.Config.Logger.WarningWithContextf(ctx, "Initial state hint was of the wrong type; expected Repository, got %T", sh)
} else {
fetchState = r
}
}
if fetchState == nil {
fetchState = rawDesired
}
// 1.2: Retrieval of raw initial state from API
rawInitial, err := c.GetRepository(ctx, fetchState)
if rawInitial == nil {
if !dcl.IsNotFound(err) {
c.Config.Logger.WarningWithContextf(ctx, "Failed to retrieve whether a Repository resource already exists: %s", err)
return nil, nil, nil, fmt.Errorf("failed to retrieve Repository resource: %v", err)
}
c.Config.Logger.InfoWithContext(ctx, "Found that Repository resource did not exist.")
// Perform canonicalization to pick up defaults.
desired, err = canonicalizeRepositoryDesiredState(rawDesired, rawInitial)
return nil, desired, nil, err
}
c.Config.Logger.InfoWithContextf(ctx, "Found initial state for Repository: %v", rawInitial)
c.Config.Logger.InfoWithContextf(ctx, "Initial desired state for Repository: %v", rawDesired)
// The Get call applies postReadExtract and so the result may contain fields that are not part of API version.
if err := extractRepositoryFields(rawInitial); err != nil {
return nil, nil, nil, err
}
// 1.3: Canonicalize raw initial state into initial state.
initial, err = canonicalizeRepositoryInitialState(rawInitial, rawDesired)
if err != nil {
return nil, nil, nil, err
}
c.Config.Logger.InfoWithContextf(ctx, "Canonicalized initial state for Repository: %v", initial)
// 1.4: Canonicalize raw desired state into desired state.
desired, err = canonicalizeRepositoryDesiredState(rawDesired, rawInitial, opts...)
if err != nil {
return nil, nil, nil, err
}
c.Config.Logger.InfoWithContextf(ctx, "Canonicalized desired state for Repository: %v", desired)
// 2.1: Comparison of initial and desired state.
diffs, err = diffRepository(c, desired, initial, opts...)
return initial, desired, diffs, err
}
func canonicalizeRepositoryInitialState(rawInitial, rawDesired *Repository) (*Repository, 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 canonicalizeRepositoryDesiredState(rawDesired, rawInitial *Repository, opts ...dcl.ApplyOption) (*Repository, 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 := &Repository{}
if dcl.PartialSelfLinkToSelfLink(rawDesired.Name, rawInitial.Name) {
canonicalDesired.Name = rawInitial.Name
} else {
canonicalDesired.Name = rawDesired.Name
}
if dcl.StringCanonicalize(rawDesired.RemoteUri, rawInitial.RemoteUri) {
canonicalDesired.RemoteUri = rawInitial.RemoteUri
} else {
canonicalDesired.RemoteUri = rawDesired.RemoteUri
}
if dcl.IsZeroValue(rawDesired.Annotations) || (dcl.IsEmptyValueIndirect(rawDesired.Annotations) && dcl.IsEmptyValueIndirect(rawInitial.Annotations)) {
// Desired and initial values are equivalent, so set canonical desired value to initial value.
canonicalDesired.Annotations = rawInitial.Annotations
} else {
canonicalDesired.Annotations = rawDesired.Annotations
}
if dcl.NameToSelfLink(rawDesired.Project, rawInitial.Project) {
canonicalDesired.Project = rawInitial.Project
} else {
canonicalDesired.Project = rawDesired.Project
}
if dcl.NameToSelfLink(rawDesired.Location, rawInitial.Location) {
canonicalDesired.Location = rawInitial.Location
} else {
canonicalDesired.Location = rawDesired.Location
}
if dcl.NameToSelfLink(rawDesired.Connection, rawInitial.Connection) {
canonicalDesired.Connection = rawInitial.Connection
} else {
canonicalDesired.Connection = rawDesired.Connection
}
return canonicalDesired, nil
}
func canonicalizeRepositoryNewState(c *Client, rawNew, rawDesired *Repository) (*Repository, error) {
if dcl.IsEmptyValueIndirect(rawNew.Name) && dcl.IsEmptyValueIndirect(rawDesired.Name) {
rawNew.Name = rawDesired.Name
} else {
if dcl.PartialSelfLinkToSelfLink(rawDesired.Name, rawNew.Name) {
rawNew.Name = rawDesired.Name
}
}
if dcl.IsEmptyValueIndirect(rawNew.RemoteUri) && dcl.IsEmptyValueIndirect(rawDesired.RemoteUri) {
rawNew.RemoteUri = rawDesired.RemoteUri
} else {
if dcl.StringCanonicalize(rawDesired.RemoteUri, rawNew.RemoteUri) {
rawNew.RemoteUri = rawDesired.RemoteUri
}
}
if dcl.IsEmptyValueIndirect(rawNew.CreateTime) && dcl.IsEmptyValueIndirect(rawDesired.CreateTime) {
rawNew.CreateTime = rawDesired.CreateTime
} else {
}
if dcl.IsEmptyValueIndirect(rawNew.UpdateTime) && dcl.IsEmptyValueIndirect(rawDesired.UpdateTime) {
rawNew.UpdateTime = rawDesired.UpdateTime
} else {
}
if dcl.IsEmptyValueIndirect(rawNew.Annotations) && dcl.IsEmptyValueIndirect(rawDesired.Annotations) {
rawNew.Annotations = rawDesired.Annotations
} else {
}
if dcl.IsEmptyValueIndirect(rawNew.Etag) && dcl.IsEmptyValueIndirect(rawDesired.Etag) {
rawNew.Etag = rawDesired.Etag
} else {
if dcl.StringCanonicalize(rawDesired.Etag, rawNew.Etag) {
rawNew.Etag = rawDesired.Etag
}
}
rawNew.Project = rawDesired.Project
rawNew.Location = rawDesired.Location
rawNew.Connection = rawDesired.Connection
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 diffRepository(c *Client, desired, actual *Repository, 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.Name, actual.Name, dcl.DiffInfo{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.RemoteUri, actual.RemoteUri, dcl.DiffInfo{OperationSelector: dcl.RequiresRecreate()}, fn.AddNest("RemoteUri")); len(ds) != 0 || err != nil {
if err != nil {
return nil, err
}
newDiffs = append(newDiffs, ds...)
}
if ds, err := dcl.Diff(desired.CreateTime, actual.CreateTime, dcl.DiffInfo{OutputOnly: true, OperationSelector: dcl.RequiresRecreate()}, fn.AddNest("CreateTime")); len(ds) != 0 || err != nil {
if err != nil {
return nil, err
}
newDiffs = append(newDiffs, ds...)
}
if ds, err := dcl.Diff(desired.UpdateTime, actual.UpdateTime, dcl.DiffInfo{OutputOnly: true, OperationSelector: dcl.RequiresRecreate()}, fn.AddNest("UpdateTime")); len(ds) != 0 || err != nil {
if err != nil {
return nil, err
}
newDiffs = append(newDiffs, ds...)
}
if ds, err := dcl.Diff(desired.Annotations, actual.Annotations, dcl.DiffInfo{OperationSelector: dcl.RequiresRecreate()}, fn.AddNest("Annotations")); len(ds) != 0 || err != nil {
if err != nil {
return nil, err
}
newDiffs = append(newDiffs, ds...)
}
if ds, err := dcl.Diff(desired.Etag, actual.Etag, dcl.DiffInfo{OutputOnly: true, OperationSelector: dcl.RequiresRecreate()}, fn.AddNest("Etag")); 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 ds, err := dcl.Diff(desired.Location, actual.Location, dcl.DiffInfo{OperationSelector: dcl.RequiresRecreate()}, fn.AddNest("Location")); len(ds) != 0 || err != nil {
if err != nil {
return nil, err
}
newDiffs = append(newDiffs, ds...)
}
if ds, err := dcl.Diff(desired.Connection, actual.Connection, dcl.DiffInfo{Type: "ReferenceType", OperationSelector: dcl.RequiresRecreate()}, fn.AddNest("Connection")); 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 *Repository) urlNormalized() *Repository {
normalized := dcl.Copy(*r).(Repository)
normalized.Name = dcl.SelfLinkToName(r.Name)
normalized.RemoteUri = dcl.SelfLinkToName(r.RemoteUri)
normalized.Etag = dcl.SelfLinkToName(r.Etag)
normalized.Project = dcl.SelfLinkToName(r.Project)
normalized.Location = dcl.SelfLinkToName(r.Location)
normalized.Connection = dcl.SelfLinkToName(r.Connection)
return &normalized
}
func (r *Repository) updateURL(userBasePath, updateName string) (string, error) {
return "", fmt.Errorf("unknown update name: %s", updateName)
}
// marshal encodes the Repository resource into JSON for a Create request, and
// performs transformations from the resource schema to the API schema if
// necessary.
func (r *Repository) marshal(c *Client) ([]byte, error) {
m, err := expandRepository(c, r)
if err != nil {
return nil, fmt.Errorf("error marshalling Repository: %w", err)
}
return json.Marshal(m)
}
// unmarshalRepository decodes JSON responses into the Repository resource schema.
func unmarshalRepository(b []byte, c *Client, res *Repository) (*Repository, error) {
var m map[string]interface{}
if err := json.Unmarshal(b, &m); err != nil {
return nil, err
}
return unmarshalMapRepository(m, c, res)
}
func unmarshalMapRepository(m map[string]interface{}, c *Client, res *Repository) (*Repository, error) {
flattened := flattenRepository(c, m, res)
if flattened == nil {
return nil, fmt.Errorf("attempted to flatten empty json object")
}
return flattened, nil
}
// expandRepository expands Repository into a JSON request object.
func expandRepository(c *Client, f *Repository) (map[string]interface{}, error) {
m := make(map[string]interface{})
res := f
_ = res
if v, err := dcl.DeriveField("projects/%s/locations/%s/connections/%s/repositories/%s", f.Name, dcl.SelfLinkToName(f.Project), dcl.SelfLinkToName(f.Location), dcl.SelfLinkToName(f.Connection), dcl.SelfLinkToName(f.Name)); err != nil {
return nil, fmt.Errorf("error expanding Name into name: %w", err)
} else if !dcl.IsEmptyValueIndirect(v) {
m["name"] = v
}
if v := f.RemoteUri; dcl.ValueShouldBeSent(v) {
m["remoteUri"] = v
}
if v := f.Annotations; dcl.ValueShouldBeSent(v) {
m["annotations"] = 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
}
if v, err := dcl.EmptyValue(); err != nil {
return nil, fmt.Errorf("error expanding Location into location: %w", err)
} else if !dcl.IsEmptyValueIndirect(v) {
m["location"] = v
}
if v, err := dcl.EmptyValue(); err != nil {
return nil, fmt.Errorf("error expanding Connection into connection: %w", err)
} else if !dcl.IsEmptyValueIndirect(v) {
m["connection"] = v
}
return m, nil
}
// flattenRepository flattens Repository from a JSON request object into the
// Repository type.
func flattenRepository(c *Client, i interface{}, res *Repository) *Repository {
m, ok := i.(map[string]interface{})
if !ok {
return nil
}
if len(m) == 0 {
return nil
}
resultRes := &Repository{}
resultRes.Name = dcl.FlattenString(m["name"])
resultRes.RemoteUri = dcl.FlattenString(m["remoteUri"])
resultRes.CreateTime = dcl.FlattenString(m["createTime"])
resultRes.UpdateTime = dcl.FlattenString(m["updateTime"])
resultRes.Annotations = dcl.FlattenKeyValuePairs(m["annotations"])
resultRes.Etag = dcl.FlattenString(m["etag"])
resultRes.Project = dcl.FlattenString(m["project"])
resultRes.Location = dcl.FlattenString(m["location"])
resultRes.Connection = dcl.FlattenString(m["connection"])
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 *Repository) matcher(c *Client) func([]byte) bool {
return func(b []byte) bool {
cr, err := unmarshalRepository(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.Location == nil && ncr.Location == nil {
c.Config.Logger.Info("Both Location fields null - considering equal.")
} else if nr.Location == nil || ncr.Location == nil {
c.Config.Logger.Info("Only one Location field is null - considering unequal.")
return false
} else if *nr.Location != *ncr.Location {
return false
}
if nr.Connection == nil && ncr.Connection == nil {
c.Config.Logger.Info("Both Connection fields null - considering equal.")
} else if nr.Connection == nil || ncr.Connection == nil {
c.Config.Logger.Info("Only one Connection field is null - considering unequal.")
return false
} else if *nr.Connection != *ncr.Connection {
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 repositoryDiff struct {
// The diff should include one or the other of RequiresRecreate or UpdateOp.
RequiresRecreate bool
UpdateOp repositoryApiOperation
FieldName string // used for error logging
}
func convertFieldDiffsToRepositoryDiffs(config *dcl.Config, fds []*dcl.FieldDiff, opts []dcl.ApplyOption) ([]repositoryDiff, 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 []repositoryDiff
// For each operation name, create a repositoryDiff which contains the operation.
for opName, fieldDiffs := range opNamesToFieldDiffs {
// Use the first field diff's field name for logging required recreate error.
diff := repositoryDiff{FieldName: fieldDiffs[0].FieldName}
if opName == "Recreate" {
diff.RequiresRecreate = true
} else {
apiOp, err := convertOpNameToRepositoryApiOperation(opName, fieldDiffs, opts...)
if err != nil {
return diffs, err
}
diff.UpdateOp = apiOp
}
diffs = append(diffs, diff)
}
return diffs, nil
}
func convertOpNameToRepositoryApiOperation(opName string, fieldDiffs []*dcl.FieldDiff, opts ...dcl.ApplyOption) (repositoryApiOperation, error) {
switch opName {
default:
return nil, fmt.Errorf("no such operation with name: %v", opName)
}
}
func extractRepositoryFields(r *Repository) error {
vProject, err := dcl.ValueFromRegexOnField("Project", r.Project, r.Connection, "projects/([a-z0-9A-Z-]*)/locations/.*")
if err != nil {
return err
}
r.Project = vProject
vLocation, err := dcl.ValueFromRegexOnField("Location", r.Location, r.Connection, "projects/.*/locations/([a-z0-9A-Z-]*)/connections/.*")
if err != nil {
return err
}
r.Location = vLocation
return nil
}
func postReadExtractRepositoryFields(r *Repository) error {
return nil
}