github.com/sacloud/iaas-api-go@v1.12.0/accessor/tags.go (about) 1 // Copyright 2022-2023 The sacloud/iaas-api-go 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 15 package accessor 16 17 import "github.com/sacloud/iaas-api-go/types" 18 19 // Tags Tagsを持つリソース向けのインターフェース 20 type Tags interface { 21 GetTags() types.Tags 22 SetTags(v types.Tags) 23 } 24 25 // HasTag 指定のタグが存在する場合trueを返す 26 func HasTag(target Tags, tag string) bool { 27 tags := target.GetTags() 28 for _, t := range tags { 29 if t == tag { 30 return true 31 } 32 } 33 return false 34 } 35 36 // AppendTag 指定のタグを追加 37 func AppendTag(target Tags, tag string) { 38 if HasTag(target, tag) { 39 return 40 } 41 tags := target.GetTags() 42 target.SetTags(append(tags, tag)) 43 } 44 45 // RemoveTag 指定のタグを削除 46 func RemoveTag(target Tags, tag string) { 47 if !HasTag(target, tag) { 48 return 49 } 50 51 tags := target.GetTags() 52 nt := types.Tags{} 53 for _, t := range tags { 54 if t != tag { 55 nt = append(nt, t) 56 } 57 } 58 target.SetTags(nt) 59 } 60 61 // ClearTags 全タグをクリア 62 func ClearTags(target Tags) { 63 target.SetTags(types.Tags{}) 64 }