github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/common/utils/logging/service/literal_loggable.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 service
    16  
    17  import "github.com/GoogleCloudPlatform/compute-image-tools/proto/go/pb"
    18  
    19  type literalLoggable struct {
    20  	strings           map[string]string
    21  	int64s            map[string][]int64
    22  	bools             map[string]bool
    23  	traceLogs         []string
    24  	inspectionResults *pb.InspectionResults
    25  }
    26  
    27  func (w literalLoggable) GetInspectionResults() *pb.InspectionResults {
    28  	return w.inspectionResults
    29  }
    30  
    31  func (w literalLoggable) GetValue(key string) string { return w.strings[key] }
    32  
    33  func (w literalLoggable) GetValueAsBool(key string) bool { return w.bools[key] }
    34  
    35  func (w literalLoggable) GetValueAsInt64Slice(key string) []int64 { return w.int64s[key] }
    36  
    37  func (w literalLoggable) ReadSerialPortLogs() []string { return w.traceLogs }
    38  
    39  // SingleImageImportLoggableBuilder initializes and builds a Loggable with the metadata
    40  // fields that are relevant when importing a single image.
    41  type SingleImageImportLoggableBuilder struct {
    42  	literalLoggable
    43  }
    44  
    45  // NewSingleImageImportLoggableBuilder creates and initializes a SingleImageImportLoggableBuilder.
    46  func NewSingleImageImportLoggableBuilder() *SingleImageImportLoggableBuilder {
    47  	return &SingleImageImportLoggableBuilder{literalLoggable{
    48  		strings: map[string]string{},
    49  		int64s:  map[string][]int64{},
    50  		bools:   map[string]bool{},
    51  	}}
    52  }
    53  
    54  // SetInspectionResults sets inspection results.
    55  func (b *SingleImageImportLoggableBuilder) SetInspectionResults(inspectionResults *pb.InspectionResults) *SingleImageImportLoggableBuilder {
    56  	b.inspectionResults = inspectionResults
    57  	return b
    58  }
    59  
    60  // SetUEFIMetrics sets UEFI related metrics.
    61  func (b *SingleImageImportLoggableBuilder) SetUEFIMetrics(isUEFICompatibleImageBool bool, isUEFIDetectedBool bool,
    62  	biosBootableBool bool, rootFSString string) *SingleImageImportLoggableBuilder {
    63  
    64  	b.bools[isUEFICompatibleImage] = isUEFICompatibleImageBool
    65  	b.bools[isUEFIDetected] = isUEFIDetectedBool
    66  	b.bools[uefiBootable] = isUEFIDetectedBool
    67  	b.bools[biosBootable] = biosBootableBool
    68  	b.strings[rootFS] = rootFSString
    69  	return b
    70  }
    71  
    72  // SetDiskAttributes sets disk related attributes.
    73  func (b *SingleImageImportLoggableBuilder) SetDiskAttributes(fileFormat string, sourceSize int64,
    74  	targetSize int64) *SingleImageImportLoggableBuilder {
    75  
    76  	b.strings[importFileFormat] = fileFormat
    77  	b.int64s[sourceSizeGb] = []int64{sourceSize}
    78  	b.int64s[targetSizeGb] = []int64{targetSize}
    79  	return b
    80  }
    81  
    82  // SetInflationAttributes sets inflation related attributes.
    83  func (b *SingleImageImportLoggableBuilder) SetInflationAttributes(matchResult string, inflationTypeStr string,
    84  	inflationTimeInt64 int64, shadowInflationTimeInt64 int64) *SingleImageImportLoggableBuilder {
    85  
    86  	b.strings[inflationType] = inflationTypeStr
    87  	b.strings[shadowDiskMatchResult] = matchResult
    88  	b.int64s[inflationTime] = []int64{inflationTimeInt64}
    89  	b.int64s[shadowInflationTime] = []int64{shadowInflationTimeInt64}
    90  	return b
    91  }
    92  
    93  // AppendTraceLogs sets trace logs during the import.
    94  func (b *SingleImageImportLoggableBuilder) AppendTraceLogs(traceLogs []string) *SingleImageImportLoggableBuilder {
    95  	if b.traceLogs != nil {
    96  		b.traceLogs = append(b.traceLogs, traceLogs...)
    97  	} else {
    98  		b.traceLogs = traceLogs
    99  	}
   100  	return b
   101  }
   102  
   103  // Build builds the actual Loggable object.
   104  func (b *SingleImageImportLoggableBuilder) Build() Loggable {
   105  	return b.literalLoggable
   106  }
   107  
   108  // OvfExportLoggableBuilder initializes and builds a Loggable with the metadata
   109  // fields that are relevant when exporting OVF.
   110  type OvfExportLoggableBuilder struct {
   111  	literalLoggable
   112  }
   113  
   114  // NewOvfExportLoggableBuilder creates and initializes a OvfExportLoggableBuilder.
   115  func NewOvfExportLoggableBuilder() *OvfExportLoggableBuilder {
   116  	return &OvfExportLoggableBuilder{literalLoggable{
   117  		strings: map[string]string{},
   118  		int64s:  map[string][]int64{},
   119  		bools:   map[string]bool{},
   120  	}}
   121  }
   122  
   123  // SetDiskSizes sets disk sizes for OVF exported disks
   124  func (b *OvfExportLoggableBuilder) SetDiskSizes(sourceSizes []int64,
   125  	targetSizes []int64) *OvfExportLoggableBuilder {
   126  
   127  	b.int64s[sourceSizeGb] = sourceSizes
   128  	b.int64s[targetSizeGb] = targetSizes
   129  	return b
   130  }
   131  
   132  // AppendTraceLogs sets trace logs during OVF export.
   133  func (b *OvfExportLoggableBuilder) AppendTraceLogs(traceLogs []string) *OvfExportLoggableBuilder {
   134  	if b.traceLogs != nil {
   135  		b.traceLogs = append(b.traceLogs, traceLogs...)
   136  	} else {
   137  		b.traceLogs = traceLogs
   138  	}
   139  	return b
   140  }
   141  
   142  // Build builds the actual Loggable object.
   143  func (b *OvfExportLoggableBuilder) Build() Loggable {
   144  	return b.literalLoggable
   145  }