github.com/go-chef/chef@v0.30.1/universe.go (about)

     1  package chef
     2  
     3  type UniverseService struct {
     4  	client *Client
     5  }
     6  
     7  // Universe represents the body of the returned information.
     8  type Universe struct {
     9  	Books map[string]UniverseBook
    10  }
    11  
    12  type UniverseBook struct {
    13  	Versions map[string]UniverseVersion
    14  }
    15  
    16  type UniverseVersion struct {
    17  	LocationPath string
    18  	LocationType string
    19  	Dependencies map[string]string
    20  }
    21  
    22  // Universe gets available cookbook version information.
    23  //
    24  // https://docs.chef.io/api_chef_server.html#universe
    25  func (e *UniverseService) Get() (universe Universe, err error) {
    26  	var data map[string]interface{}
    27  	err = e.client.magicRequestDecoder("GET", "universe", nil, &data)
    28  	unpackUniverse(&universe, &data)
    29  	return
    30  }
    31  
    32  func unpackUniverse(universe *Universe, data *map[string]interface{}) {
    33  	(*universe).Books = make(map[string]UniverseBook)
    34  	for bookn, versions := range *data {
    35  		ub := UniverseBook{}
    36  		ub.Versions = make(map[string]UniverseVersion)
    37  		switch versions.(type) {
    38  		case map[string]interface{}:
    39  			for vname, version := range versions.(map[string]interface{}) {
    40  				uv := UniverseVersion{}
    41  				switch version.(type) {
    42  				case map[string]interface{}:
    43  					for aname, attr := range version.(map[string]interface{}) {
    44  						deps := make(map[string]string)
    45  						switch aname {
    46  						case "dependencies":
    47  							for dname, dep := range attr.(map[string]interface{}) {
    48  								switch dep.(type) {
    49  								case string:
    50  									deps[dname] = dep.(string)
    51  								default:
    52  								}
    53  							}
    54  							uv.Dependencies = deps
    55  						case "location_path":
    56  							uv.LocationPath = attr.(string)
    57  						case "location_type":
    58  							uv.LocationType = attr.(string)
    59  						}
    60  					}
    61  				default:
    62  				}
    63  				ub.Versions[vname] = uv
    64  			}
    65  		default:
    66  		}
    67  		(*universe).Books[bookn] = ub
    68  	}
    69  	return
    70  }