github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/cis/cisv1/glbs.go (about) 1 package cisv1 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/IBM-Cloud/bluemix-go/client" 8 ) 9 10 type Glb struct { 11 Id string `json:"id"` 12 Name string `json:"name"` 13 Desc string `json:"description"` 14 FallbackPool string `json:"fallback_pool"` 15 DefaultPools []string `json:"default_pools"` 16 Ttl int `json:"ttl"` 17 Proxied bool `json:"proxied"` 18 CreatedOn *time.Time `json:"created_on,omitempty"` 19 ModifiedOn *time.Time `json:"modified_on,omitempty"` 20 SessionAffinity string `json:"session_affinity"` 21 Enabled bool `json:"enabled,omitempty"` 22 RegionPools map[string][]string `json:"region_pools,omitempty"` 23 PopPools map[string][]string `json:"pop_pools,omitempty"` 24 } 25 26 type GlbResults struct { 27 GlbList []Glb `json:"result"` 28 ResultsInfo ResultsCount `json:"result_info"` 29 Success bool `json:"success"` 30 Errors []Error `json:"errors"` 31 } 32 33 type GlbResult struct { 34 Glb Glb `json:"result"` 35 Success bool `json:"success"` 36 Errors []Error `json:"errors"` 37 Messages []string `json:"messages"` 38 } 39 40 type GlbBody struct { 41 Desc string `json:"description,omitempty"` 42 Proxied bool `json:"proxied,omitempty"` 43 Name string `json:"name"` 44 FallbackPool string `json:"fallback_pool"` 45 DefaultPools []string `json:"default_pools"` 46 SessionAffinity string `json:"session_affinity,omitempty"` 47 Ttl int `json:"ttl,omitempty"` 48 Enabled bool `json:"enabled,omitempty"` 49 RegionPools map[string][]string `json:"region_pools,omitempty"` 50 PopPools map[string][]string `json:"pop_pools,omitempty"` 51 } 52 53 type GlbDelete struct { 54 Result struct { 55 GlbId string 56 } `json:"result"` 57 Success bool `json:"success"` 58 Errors []Error `json:"errors"` 59 Messages []string `json:"messages"` 60 } 61 62 type Glbs interface { 63 ListGlbs(cisId string, zoneId string) ([]Glb, error) 64 GetGlb(cisId string, zoneId string, glbId string) (*Glb, error) 65 CreateGlb(cisId string, zoneId string, glbBody GlbBody) (*Glb, error) 66 DeleteGlb(cisId string, zoneId string, glbId string) error 67 UpdateGlb(cisId string, zoneId string, glbId string, glbBody GlbBody) (*Glb, error) 68 } 69 70 type glbs struct { 71 client *client.Client 72 } 73 74 func newGlbAPI(c *client.Client) Glbs { 75 return &glbs{ 76 client: c, 77 } 78 } 79 80 func (r *glbs) ListGlbs(cisId string, zoneId string) ([]Glb, error) { 81 glbResults := GlbResults{} 82 rawURL := fmt.Sprintf("/v1/%s/zones/%s/load_balancers", cisId, zoneId) 83 _, err := r.client.Get(rawURL, &glbResults) 84 if err != nil { 85 return nil, err 86 } 87 return glbResults.GlbList, err 88 } 89 90 func (r *glbs) GetGlb(cisId string, zoneId string, glbId string) (*Glb, error) { 91 glbResult := GlbResult{} 92 rawURL := fmt.Sprintf("/v1/%s/zones/%s/load_balancers/%s", cisId, zoneId, glbId) 93 _, err := r.client.Get(rawURL, &glbResult, nil) 94 if err != nil { 95 return nil, err 96 } 97 return &glbResult.Glb, nil 98 } 99 100 func (r *glbs) DeleteGlb(cisId string, zoneId string, glbId string) error { 101 rawURL := fmt.Sprintf("/v1/%s/zones/%s/load_balancers/%s", cisId, zoneId, glbId) 102 _, err := r.client.Delete(rawURL) 103 if err != nil { 104 return err 105 } 106 return nil 107 } 108 109 func (r *glbs) CreateGlb(cisId string, zoneId string, glbBody GlbBody) (*Glb, error) { 110 glbResult := GlbResult{} 111 rawURL := fmt.Sprintf("/v1/%s/zones/%s/load_balancers", cisId, zoneId) 112 _, err := r.client.Post(rawURL, &glbBody, &glbResult) 113 if err != nil { 114 return nil, err 115 } 116 return &glbResult.Glb, nil 117 } 118 119 func (r *glbs) UpdateGlb(cisId string, zoneId string, glbId string, glbBody GlbBody) (*Glb, error) { 120 glbResult := GlbResult{} 121 rawURL := fmt.Sprintf("/v1/%s/zones/%s/load_balancers/%s", cisId, zoneId, glbId) 122 _, err := r.client.Put(rawURL, &glbBody, &glbResult) 123 if err != nil { 124 return nil, err 125 } 126 return &glbResult.Glb, nil 127 }