yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/openstack/loadbalbancerhealthmonitor.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package openstack 16 17 import ( 18 "fmt" 19 20 "yunion.io/x/jsonutils" 21 "yunion.io/x/pkg/errors" 22 23 api "yunion.io/x/cloudmux/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 type SLoadbalancerHealthmonitorCreateParams struct { 29 Name string `json:"name,omitempty"` 30 AdminStateUp bool `json:"admin_state_up"` 31 PoolID string `json:"pool_id,omitempty"` 32 Delay *int `json:"delay,omitempty"` 33 ExpectedCodes string `json:"expected_codes,omitempty"` 34 MaxRetries *int `json:"max_retries,omitempty"` 35 HTTPMethod string `json:"http_method,omitempty"` 36 Timeout *int `json:"timeout,omitempty"` 37 URLPath string `json:"url_path,omitempty"` 38 Type string `json:"type,omitempty"` 39 MaxRetriesDown *int `json:"max_retries_down,omitempty"` 40 Tags []string `json:"tags,omitempty"` 41 HTTPVersion *float64 `json:"http_version,omitempty"` 42 DomainName string `json:"domain_name,omitempty"` 43 } 44 45 type SLoadbalancerHealthmonitorUpdateParams struct { 46 Name string `json:"name,omitempty"` 47 AdminStateUp bool `json:"admin_state_up"` 48 Delay *int `json:"delay,omitempty"` 49 ExpectedCodes string `json:"expected_codes,omitempty"` 50 MaxRetries *int `json:"max_retries,omitempty"` 51 HTTPMethod string `json:"http_method,omitempty"` 52 Timeout *int `json:"timeout,omitempty"` 53 URLPath string `json:"url_path,omitempty"` 54 MaxRetriesDown *int `json:"max_retries_down,omitempty"` 55 Tags []string `json:"tags,omitempty"` 56 HTTPVersion *float64 `json:"http_version,omitempty"` 57 DomainName string `json:"domain_name,omitempty"` 58 } 59 60 type SLoadbalancerHealthmonitor struct { 61 multicloud.SResourceBase 62 OpenStackTags 63 region *SRegion 64 ProjectID string `json:"project_id"` 65 Name string `json:"name"` 66 AdminStateUp bool `json:"admin_state_up"` 67 PoolIds []SPoolID `json:"pools"` 68 CreatedAt string `json:"created_at"` 69 ProvisioningStatus string `json:"provisioning_status"` 70 UpdatedAt string `json:"updated_at"` 71 Delay int `json:"delay"` 72 ExpectedCodes string `json:"expected_codes"` 73 MaxRetries int `json:"max_retries"` 74 HTTPMethod string `json:"http_method"` 75 Timeout int `json:"timeout"` 76 MaxRetriesDown int `json:"max_retries_down"` 77 URLPath string `json:"url_path"` 78 Type string `json:"type"` 79 ID string `json:"id"` 80 OperatingStatus string `json:"operating_status"` 81 Tags []string `json:"tags"` 82 HTTPVersion float64 `json:"http_version"` 83 DomainName string `json:"domain_name"` 84 } 85 86 func (region *SRegion) GetLoadbalancerHealthmonitorById(healthmonitorId string) (*SLoadbalancerHealthmonitor, error) { 87 body, err := region.lbGet(fmt.Sprintf("/v2/lbaas/healthmonitors/%s", healthmonitorId)) 88 if err != nil { 89 return nil, errors.Wrapf(err, `region.lbGet(/v2/lbaas/healthmonitors/%s)`, healthmonitorId) 90 } 91 healthmonitor := SLoadbalancerHealthmonitor{} 92 healthmonitor.region = region 93 return &healthmonitor, body.Unmarshal(&healthmonitor, "healthmonitor") 94 } 95 func (region *SRegion) CreateLoadbalancerHealthmonitor(poolId string, healthcheck *cloudprovider.SLoadbalancerHealthCheck) (*SLoadbalancerHealthmonitor, error) { 96 type CreateParams struct { 97 Healthmonitor SLoadbalancerHealthmonitorCreateParams `json:"healthmonitor"` 98 } 99 params := CreateParams{} 100 params.Healthmonitor.AdminStateUp = true 101 params.Healthmonitor.Delay = &healthcheck.HealthCheckInterval 102 params.Healthmonitor.Timeout = &healthcheck.HealthCheckTimeout 103 104 params.Healthmonitor.MaxRetries = &healthcheck.HealthCheckRise 105 params.Healthmonitor.MaxRetriesDown = &healthcheck.HealthCheckFail 106 params.Healthmonitor.PoolID = poolId 107 108 switch healthcheck.HealthCheckType { 109 case api.LB_HEALTH_CHECK_TCP: 110 params.Healthmonitor.Type = "TCP" 111 case api.LB_HEALTH_CHECK_UDP: 112 params.Healthmonitor.Type = "UDP-CONNECT" 113 case api.LB_HEALTH_CHECK_HTTP: 114 params.Healthmonitor.Type = "HTTP" 115 case api.LB_HEALTH_CHECK_HTTPS: 116 params.Healthmonitor.Type = "HTTPS" 117 case api.LB_HEALTH_CHECK_PING: 118 params.Healthmonitor.Type = "PING" 119 default: 120 params.Healthmonitor.Type = "PING" 121 } 122 123 if params.Healthmonitor.Type == "HTTP" || params.Healthmonitor.Type == "HTTPS" { 124 params.Healthmonitor.HTTPMethod = "GET" 125 httpVersion := 1.1 126 params.Healthmonitor.HTTPVersion = &httpVersion 127 params.Healthmonitor.DomainName = healthcheck.HealthCheckDomain 128 params.Healthmonitor.URLPath = healthcheck.HealthCheckURI 129 params.Healthmonitor.ExpectedCodes = ToOpenstackHealthCheckHttpCode(healthcheck.HealthCheckHttpCode) 130 } 131 132 body, err := region.lbPost("/v2/lbaas/healthmonitors", jsonutils.Marshal(params)) 133 if err != nil { 134 return nil, errors.Wrap(err, `region.lbPost("/v2/lbaas/healthmonitors", jsonutils.Marshal(params))`) 135 } 136 shealthmonitor := SLoadbalancerHealthmonitor{} 137 shealthmonitor.region = region 138 err = body.Unmarshal(&shealthmonitor, "healthmonitor") 139 if err != nil { 140 return nil, errors.Wrap(err, "body.Unmarshal(&shealthmonitor, healthmonitor)") 141 } 142 return &shealthmonitor, nil 143 } 144 145 func (region *SRegion) UpdateLoadbalancerHealthmonitor(healthmonitorId string, healthcheck *cloudprovider.SLoadbalancerHealthCheck) (*SLoadbalancerHealthmonitor, error) { 146 type UpdateParams struct { 147 Healthmonitor SLoadbalancerHealthmonitorUpdateParams `json:"healthmonitor"` 148 } 149 params := UpdateParams{} 150 params.Healthmonitor.AdminStateUp = true 151 params.Healthmonitor.Delay = &healthcheck.HealthCheckInterval 152 params.Healthmonitor.Timeout = &healthcheck.HealthCheckTimeout 153 154 params.Healthmonitor.MaxRetries = &healthcheck.HealthCheckRise 155 params.Healthmonitor.MaxRetriesDown = &healthcheck.HealthCheckFail 156 157 if healthcheck.HealthCheckType == api.LB_HEALTH_CHECK_HTTP || healthcheck.HealthCheckType == api.LB_HEALTH_CHECK_HTTPS { 158 params.Healthmonitor.HTTPMethod = "GET" 159 httpVersion := 1.1 160 params.Healthmonitor.HTTPVersion = &httpVersion 161 params.Healthmonitor.DomainName = healthcheck.HealthCheckDomain 162 params.Healthmonitor.URLPath = healthcheck.HealthCheckURI 163 params.Healthmonitor.ExpectedCodes = ToOpenstackHealthCheckHttpCode(healthcheck.HealthCheckHttpCode) 164 } 165 166 body, err := region.lbUpdate(fmt.Sprintf("/v2/lbaas/healthmonitors/%s", healthmonitorId), jsonutils.Marshal(params)) 167 if err != nil { 168 return nil, errors.Wrapf(err, `region.lbUpdate(/v2/lbaas/healthmonitors/%s), jsonutils.Marshal(params))`, healthmonitorId) 169 } 170 shealthmonitor := SLoadbalancerHealthmonitor{} 171 err = body.Unmarshal(&shealthmonitor, "healthmonitor") 172 shealthmonitor.region = region 173 if err != nil { 174 return nil, errors.Wrap(err, "body.Unmarshal(&shealthmonitor, healthmonitor)") 175 } 176 return &shealthmonitor, nil 177 } 178 179 func (region *SRegion) DeleteLoadbalancerHealthmonitor(healthmonitorId string) error { 180 _, err := region.lbDelete(fmt.Sprintf("/v2/lbaas/healthmonitors/%s", healthmonitorId)) 181 if err != nil { 182 return errors.Wrapf(err, `region.lbDelete(/v2/lbaas/healthmonitors/%s )`, healthmonitorId) 183 } 184 return nil 185 } 186 187 func (healthmonitor *SLoadbalancerHealthmonitor) GetName() string { 188 return healthmonitor.Name 189 } 190 191 func (healthmonitor *SLoadbalancerHealthmonitor) GetId() string { 192 return healthmonitor.ID 193 } 194 195 func (healthmonitor *SLoadbalancerHealthmonitor) GetGlobalId() string { 196 return healthmonitor.ID 197 } 198 199 func (healthmonitor *SLoadbalancerHealthmonitor) GetStatus() string { 200 switch healthmonitor.ProvisioningStatus { 201 case "ACTIVE": 202 return api.LB_STATUS_ENABLED 203 case "PENDING_CREATE": 204 return api.LB_CREATING 205 case "PENDING_UPDATE": 206 return api.LB_SYNC_CONF 207 case "PENDING_DELETE": 208 return api.LB_STATUS_DELETING 209 case "DELETED": 210 return api.LB_STATUS_DELETED 211 default: 212 return api.LB_STATUS_UNKNOWN 213 } 214 } 215 216 func (healthmonitor *SLoadbalancerHealthmonitor) Refresh() error { 217 newhealthmonitor, err := healthmonitor.region.GetLoadbalancerHealthmonitorById(healthmonitor.ID) 218 if err != nil { 219 return err 220 } 221 return jsonutils.Update(healthmonitor, newhealthmonitor) 222 } 223 224 func (healthmonitor *SLoadbalancerHealthmonitor) IsEmulated() bool { 225 return false 226 }