1// Copyright 2016-2020 The Libsacloud Authors 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 15package api 16 17import ( 18 "encoding/json" 19 "fmt" 20 "net/url" 21 "strings" 22 23 "github.com/sacloud/libsacloud/sacloud" 24) 25 26// Reset 検索条件のリセット 27func (api *WebAccelAPI) Reset() *WebAccelAPI { 28 api.SetEmpty() 29 return api 30} 31 32// SetEmpty 検索条件のリセット 33func (api *WebAccelAPI) SetEmpty() { 34 api.reset() 35} 36 37// FilterBy 指定キーでのフィルター 38func (api *WebAccelAPI) FilterBy(key string, value interface{}) *WebAccelAPI { 39 api.filterBy(key, value, false) 40 return api 41} 42 43// WithNameLike 名称条件 44func (api *WebAccelAPI) WithNameLike(name string) *WebAccelAPI { 45 return api.FilterBy("Name", name) 46} 47 48// SetFilterBy 指定キーでのフィルター 49func (api *WebAccelAPI) SetFilterBy(key string, value interface{}) { 50 api.filterBy(key, value, false) 51} 52 53// SetNameLike 名称条件 54func (api *WebAccelAPI) SetNameLike(name string) { 55 api.FilterBy("Name", name) 56} 57 58// Find サイト一覧取得 59func (api *WebAccelAPI) Find() (*sacloud.SearchResponse, error) { 60 61 uri := fmt.Sprintf("%s/site", api.getResourceURL()) 62 63 data, err := api.client.newRequest("GET", uri, nil) 64 if err != nil { 65 return nil, err 66 } 67 68 var res sacloud.SearchResponse 69 if err := json.Unmarshal(data, &res); err != nil { 70 return nil, err 71 } 72 73 // handle filter(API側がFilterに対応していないためここでフィルタリング) 74 for key, filter := range api.getSearchState().Filter { 75 if key != "Name" { 76 continue 77 } 78 strNames, ok := filter.(string) 79 if !ok { 80 continue 81 } 82 83 names := strings.Split(strNames, " ") 84 filtered := []sacloud.WebAccelSite{} 85 for _, site := range res.WebAccelSites { 86 for _, name := range names { 87 88 u, _ := url.Parse(name) 89 90 if strings.Contains(site.Name, u.Path) { 91 filtered = append(filtered, site) 92 break 93 } 94 } 95 } 96 res.WebAccelSites = filtered 97 } 98 99 return &res, nil 100} 101