Debian Codegraph @

catalog.go

raw symbols SCIP
1 /*
2 * Copyright 2014 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
3 */
4
5 package govcd
6
7 import (
8 "fmt"
9 "net/url"
10
11 types "github.com/hmrc/vmware-govcd/types/v56"
12 )
13
14 type Catalog struct {
15 Catalog *types.Catalog
16 c *Client
17 }
18
19 func NewCatalog(c *Client) *Catalog {
20 return &Catalog{
21 Catalog: new(types.Catalog),
22 c: c,
23 }
24 }
25
26 func (c *Catalog) FindCatalogItem(catalogitem string) (CatalogItem, error) {
27
28 for _, cis := range c.Catalog.CatalogItems {
29 for _, ci := range cis.CatalogItem {
30 if ci.Name == catalogitem && ci.Type == "application/vnd.vmware.vcloud.catalogItem+xml" {
31 u, err := url.ParseRequestURI(ci.HREF)
32
33 if err != nil {
34 return CatalogItem{}, fmt.Errorf("error decoding catalog response: %s", err)
35 }
36
37 req := c.c.NewRequest(map[string]string{}, "GET", *u, nil)
38
39 resp, err := checkResp(c.c.Http.Do(req))
40 if err != nil {
41 return CatalogItem{}, fmt.Errorf("error retreiving catalog: %s", err)
42 }
43
44 cat := NewCatalogItem(c.c)
45
46 if err = decodeBody(resp, cat.CatalogItem); err != nil {
47 return CatalogItem{}, fmt.Errorf("error decoding catalog response: %s", err)
48 }
49
50 // The request was successful
51 return *cat, nil
52 }
53 }
54 }
55
56 return CatalogItem{}, fmt.Errorf("can't find catalog item: %s", catalogitem)
57 }