github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/boskos/common/mason_config.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package common
    18  
    19  import (
    20  	"fmt"
    21  )
    22  
    23  // ResourceNeeds maps the type to count of resources types needed
    24  type ResourceNeeds map[string]int
    25  
    26  // TypeToResources stores all the leased resources with the same type f
    27  type TypeToResources map[string][]Resource
    28  
    29  // ConfigType gather the type of config to be applied by Mason in order to construct the resource
    30  type ConfigType struct {
    31  	// Identifier of the struct this maps back to
    32  	Type string `json:"type,omitempty"`
    33  	// Marshaled JSON content
    34  	Content string `json:"content,omitempty"`
    35  }
    36  
    37  // MasonConfig holds Mason config information
    38  type MasonConfig struct {
    39  	Configs []ResourcesConfig `json:"configs,flow,omitempty"`
    40  }
    41  
    42  // ResourcesConfig holds information to construct a resource.
    43  // The ResourcesConfig Name maps to the Resource Type
    44  // All Resource of a given type will be constructed using the same configuration
    45  type ResourcesConfig struct {
    46  	Name   string        `json:"name"`
    47  	Config ConfigType    `json:"config"`
    48  	Needs  ResourceNeeds `json:"needs"`
    49  }
    50  
    51  // ResourcesConfigByName helps sorting ResourcesConfig by name
    52  type ResourcesConfigByName []ResourcesConfig
    53  
    54  func (ut ResourcesConfigByName) Len() int           { return len(ut) }
    55  func (ut ResourcesConfigByName) Swap(i, j int)      { ut[i], ut[j] = ut[j], ut[i] }
    56  func (ut ResourcesConfigByName) Less(i, j int) bool { return ut[i].GetName() < ut[j].GetName() }
    57  
    58  // GetName implements the item interface for storage
    59  func (conf ResourcesConfig) GetName() string { return conf.Name }
    60  
    61  // ItemToResourcesConfig casts an Item object to a ResourcesConfig
    62  func ItemToResourcesConfig(i Item) (ResourcesConfig, error) {
    63  	conf, ok := i.(ResourcesConfig)
    64  	if !ok {
    65  		return ResourcesConfig{}, fmt.Errorf("cannot construct Resource from received object %v", i)
    66  	}
    67  	return conf, nil
    68  }
    69  
    70  // Copy returns a copy of the TypeToResources
    71  func (t TypeToResources) Copy() TypeToResources {
    72  	n := TypeToResources{}
    73  	for k, v := range t {
    74  		n[k] = v
    75  	}
    76  	return n
    77  }