github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/utilization/pcf.go (about) 1 package utilization 2 3 import ( 4 "errors" 5 "fmt" 6 "net/http" 7 "os" 8 ) 9 10 type pcf struct { 11 InstanceGUID string `json:"cf_instance_guid,omitempty"` 12 InstanceIP string `json:"cf_instance_ip,omitempty"` 13 MemoryLimit string `json:"memory_limit,omitempty"` 14 } 15 16 func gatherPCF(util *Data, _ *http.Client) error { 17 pcf, err := getPCF(os.Getenv) 18 if err != nil { 19 // Only return the error here if it is unexpected to prevent 20 // warning customers who aren't running PCF about a timeout. 21 if _, ok := err.(unexpectedPCFErr); ok { 22 return err 23 } 24 return nil 25 } 26 util.Vendors.PCF = pcf 27 28 return nil 29 } 30 31 type unexpectedPCFErr struct{ e error } 32 33 func (e unexpectedPCFErr) Error() string { 34 return fmt.Sprintf("unexpected PCF error: %v", e.e) 35 } 36 37 var ( 38 errNoPCFVariables = errors.New("no PCF environment variables present") 39 ) 40 41 func getPCF(initializer func(key string) string) (*pcf, error) { 42 p := &pcf{} 43 44 p.InstanceGUID = initializer("CF_INSTANCE_GUID") 45 p.InstanceIP = initializer("CF_INSTANCE_IP") 46 p.MemoryLimit = initializer("MEMORY_LIMIT") 47 48 if "" == p.InstanceGUID && "" == p.InstanceIP && "" == p.MemoryLimit { 49 return nil, errNoPCFVariables 50 } 51 52 if err := p.validate(); err != nil { 53 return nil, unexpectedPCFErr{e: err} 54 } 55 56 return p, nil 57 } 58 59 func (pcf *pcf) validate() (err error) { 60 pcf.InstanceGUID, err = normalizeValue(pcf.InstanceGUID) 61 if err != nil { 62 return fmt.Errorf("Invalid instance GUID: %v", err) 63 } 64 65 pcf.InstanceIP, err = normalizeValue(pcf.InstanceIP) 66 if err != nil { 67 return fmt.Errorf("Invalid instance IP: %v", err) 68 } 69 70 pcf.MemoryLimit, err = normalizeValue(pcf.MemoryLimit) 71 if err != nil { 72 return fmt.Errorf("Invalid memory limit: %v", err) 73 } 74 75 if pcf.InstanceGUID == "" || pcf.InstanceIP == "" || pcf.MemoryLimit == "" { 76 err = errors.New("One or more environment variables are unavailable") 77 } 78 79 return 80 }