github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/common/utils/param/populator.go (about)

     1  //  Copyright 2020 Google Inc. All Rights Reserved.
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package param
    16  
    17  import (
    18  	"github.com/GoogleCloudPlatform/compute-image-tools/cli_tools/common/domain"
    19  )
    20  
    21  // Populator standardizes user input, and determines omitted values.
    22  type Populator interface {
    23  	PopulateMissingParameters(project *string, clientID string, zone *string, region *string,
    24  		scratchBucketGcsPath *string, file string, storageLocation, network, subnet *string) error
    25  }
    26  
    27  // NewPopulator returns an object that implements Populator.
    28  func NewPopulator(
    29  	NetworkResolver NetworkResolver,
    30  	metadataClient domain.MetadataGCEInterface,
    31  	storageClient domain.StorageClientInterface,
    32  	locationClient domain.ResourceLocationRetrieverInterface,
    33  	scratchBucketClient domain.ScratchBucketCreatorInterface) Populator {
    34  	return &populator{
    35  		NetworkResolver:     NetworkResolver,
    36  		metadataClient:      metadataClient,
    37  		storageClient:       storageClient,
    38  		locationClient:      locationClient,
    39  		scratchBucketClient: scratchBucketClient,
    40  	}
    41  }
    42  
    43  type populator struct {
    44  	NetworkResolver     NetworkResolver
    45  	metadataClient      domain.MetadataGCEInterface
    46  	storageClient       domain.StorageClientInterface
    47  	locationClient      domain.ResourceLocationRetrieverInterface
    48  	scratchBucketClient domain.ScratchBucketCreatorInterface
    49  }
    50  
    51  func (p *populator) PopulateMissingParameters(project *string, clientID string, zone *string,
    52  	region *string, scratchBucketGcsPath *string, file string, storageLocation, network, subnet *string) error {
    53  
    54  	if err := PopulateProjectIfMissing(p.metadataClient, project); err != nil {
    55  		return err
    56  	}
    57  
    58  	scratchBucketRegion, err := populateScratchBucketGcsPath(scratchBucketGcsPath, *zone, p.metadataClient,
    59  		p.scratchBucketClient, file, project, p.storageClient, clientID == "gcloud")
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	if storageLocation != nil && *storageLocation == "" {
    65  		*storageLocation = p.locationClient.GetLargestStorageLocation(scratchBucketRegion)
    66  	}
    67  
    68  	if *zone == "" {
    69  		if *zone, err = p.locationClient.GetZone(scratchBucketRegion, *project); err != nil {
    70  			return err
    71  		}
    72  	}
    73  
    74  	if err := PopulateRegion(region, *zone); err != nil {
    75  		return err
    76  	}
    77  
    78  	if *network, *subnet, err = p.NetworkResolver.Resolve(*network, *subnet, *region, *project); err != nil {
    79  		return err
    80  	}
    81  
    82  	return nil
    83  }