yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/loadbalancerlistenerrule.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 azure
    16  
    17  import (
    18  	"context"
    19  	"net/url"
    20  	"regexp"
    21  	"strings"
    22  
    23  	"yunion.io/x/log"
    24  	"yunion.io/x/pkg/errors"
    25  
    26  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    27  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    28  	"yunion.io/x/cloudmux/pkg/multicloud"
    29  )
    30  
    31  type SLoadbalancerListenerRule struct {
    32  	multicloud.SResourceBase
    33  	multicloud.SLoadbalancerRedirectBase
    34  
    35  	listener *SLoadBalancerListener
    36  	lbbg     *SLoadbalancerBackendGroup
    37  	redirect *RedirectConfiguration
    38  	// urlPathMaps -> pathRules
    39  	Name       string             `json:"name"`
    40  	ID         string             `json:"id"`
    41  	Domain     string             `json:"domain"`
    42  	Properties PathRuleProperties `json:"properties"`
    43  }
    44  
    45  func (self *SLoadbalancerListenerRule) GetId() string {
    46  	return self.ID
    47  }
    48  
    49  func (self *SLoadbalancerListenerRule) GetName() string {
    50  	return self.Name
    51  }
    52  
    53  func (self *SLoadbalancerListenerRule) GetGlobalId() string {
    54  	return strings.ToLower(self.GetId())
    55  }
    56  
    57  func (self *SLoadbalancerListenerRule) GetStatus() string {
    58  	switch self.Properties.ProvisioningState {
    59  	case "Succeeded":
    60  		return api.LB_STATUS_ENABLED
    61  	case "Failed":
    62  		return api.LB_STATUS_DISABLED
    63  	default:
    64  		return api.LB_STATUS_UNKNOWN
    65  	}
    66  }
    67  
    68  func (self *SLoadbalancerListenerRule) GetSysTags() map[string]string {
    69  	return nil
    70  }
    71  
    72  func (self *SLoadbalancerListenerRule) GetTags() (map[string]string, error) {
    73  	return nil, nil
    74  }
    75  
    76  func (self *SLoadbalancerListenerRule) SetTags(tags map[string]string, replace bool) error {
    77  	return errors.Wrap(cloudprovider.ErrNotImplemented, "SetTags")
    78  }
    79  
    80  func (self *SLoadbalancerListenerRule) GetProjectId() string {
    81  	return getResourceGroup(self.GetId())
    82  }
    83  
    84  func (self *SLoadbalancerListenerRule) IsDefault() bool {
    85  	return false
    86  }
    87  
    88  func (self *SLoadbalancerListenerRule) GetDomain() string {
    89  	return self.Domain
    90  }
    91  
    92  func (self *SLoadbalancerListenerRule) GetPath() string {
    93  	if len(self.Properties.Paths) > 0 {
    94  		return self.Properties.Paths[0]
    95  	}
    96  
    97  	return ""
    98  }
    99  
   100  func (self *SLoadbalancerListenerRule) GetCondition() string {
   101  	return ""
   102  }
   103  
   104  func (self *SLoadbalancerListenerRule) GetBackendGroupId() string {
   105  	if self.lbbg != nil {
   106  		return self.lbbg.GetId()
   107  	}
   108  
   109  	return ""
   110  }
   111  
   112  func (self *SLoadbalancerListenerRule) Delete(ctx context.Context) error {
   113  	return errors.Wrap(cloudprovider.ErrNotImplemented, "Delete")
   114  }
   115  
   116  func (self *SLoadbalancerListenerRule) GetRedirect() string {
   117  	if self.redirect != nil {
   118  		return api.LB_REDIRECT_RAW
   119  	}
   120  
   121  	return api.LB_REDIRECT_OFF
   122  }
   123  
   124  func (self *SLoadbalancerListenerRule) GetRedirectCode() int64 {
   125  	if self.redirect == nil {
   126  		return 0
   127  	}
   128  
   129  	switch self.redirect.Properties.RedirectType {
   130  	case "Permanent":
   131  		return api.LB_REDIRECT_CODE_301
   132  	case "Found":
   133  		return api.LB_REDIRECT_CODE_302
   134  	case "Temporary", "SeeOther":
   135  		return api.LB_REDIRECT_CODE_307
   136  	default:
   137  		return 0
   138  	}
   139  }
   140  
   141  func (self *SLoadbalancerListenerRule) getRedirectUrl() *url.URL {
   142  	if self.redirect == nil {
   143  		return nil
   144  	}
   145  
   146  	if len(self.redirect.Properties.TargetUrl) == 0 {
   147  		return nil
   148  	}
   149  
   150  	_url := self.redirect.Properties.TargetUrl
   151  	if matched, _ := regexp.MatchString("^\\w{0,5}://", _url); !matched {
   152  		_url = "http://" + _url
   153  	}
   154  
   155  	u, err := url.Parse(_url)
   156  	if err != nil {
   157  		log.Debugf("url Parse %s : %s", self.redirect.Properties.TargetUrl, err)
   158  		return nil
   159  	}
   160  
   161  	return u
   162  }
   163  func (self *SLoadbalancerListenerRule) GetRedirectScheme() string {
   164  	u := self.getRedirectUrl()
   165  	if u == nil {
   166  		return ""
   167  	}
   168  
   169  	return strings.ToLower(u.Scheme)
   170  }
   171  
   172  func (self *SLoadbalancerListenerRule) GetRedirectHost() string {
   173  	u := self.getRedirectUrl()
   174  	if u == nil {
   175  		if self.redirect != nil && len(self.redirect.Properties.TargetListener.ID) > 0 {
   176  			segs := strings.Split(self.redirect.Properties.TargetListener.ID, "/")
   177  			return segs[len(segs)-1]
   178  		}
   179  		return ""
   180  	}
   181  
   182  	return u.Host
   183  }
   184  
   185  func (self *SLoadbalancerListenerRule) GetRedirectPath() string {
   186  	u := self.getRedirectUrl()
   187  	if u == nil {
   188  		return ""
   189  	}
   190  
   191  	return u.Path
   192  }