github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/panos/helpers.go (about) 1 // Copyright 2018 The Terraformer 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 panos 16 17 import ( 18 "fmt" 19 "strings" 20 "unicode" 21 22 "github.com/PaloAltoNetworks/pango" 23 "golang.org/x/text/secure/precis" 24 "golang.org/x/text/transform" 25 "golang.org/x/text/unicode/norm" 26 ) 27 28 func Initialize() (interface{}, error) { 29 return pango.Connect(pango.Client{ 30 CheckEnvironment: true, 31 }) 32 } 33 34 func GetVsysList() ([]string, interface{}, error) { 35 client, err := Initialize() 36 if err != nil { 37 return []string{}, nil, err 38 } 39 40 switch c := client.(type) { 41 case *pango.Panorama: 42 return []string{"shared"}, pango.Panorama{}, nil 43 case *pango.Firewall: 44 var vsysList []string 45 vsysList, err = c.Vsys.GetList() 46 return vsysList, pango.Firewall{}, err 47 } 48 49 return []string{}, nil, fmt.Errorf("client type not supported") 50 } 51 52 func FilterCallableResources(t interface{}, resources []string) []string { 53 var filteredResources []string 54 55 switch t.(type) { 56 case pango.Panorama: 57 for _, r := range resources { 58 if strings.HasPrefix(r, "panorama_") { 59 filteredResources = append(filteredResources, r) 60 } 61 } 62 case pango.Firewall: 63 for _, r := range resources { 64 if strings.HasPrefix(r, "firewall_") { 65 filteredResources = append(filteredResources, r) 66 } 67 } 68 } 69 70 return filteredResources 71 } 72 73 func normalizeResourceName(s string) string { 74 normalize := precis.NewIdentifier( 75 precis.AdditionalMapping(func() transform.Transformer { 76 return transform.Chain(norm.NFD, transform.RemoveFunc(func(r rune) bool { //nolint 77 return unicode.Is(unicode.Mn, r) 78 })) 79 }), 80 precis.Norm(norm.NFC), 81 ) 82 83 r := strings.NewReplacer(" ", "_", 84 "!", "_", 85 "\"", "_", 86 "#", "_", 87 "%", "_", 88 "&", "_", 89 "'", "_", 90 "(", "_", 91 ")", "_", 92 "{", "_", 93 "}", "_", 94 "*", "_", 95 "+", "_", 96 ",", "_", 97 "-", "_", 98 ".", "_", 99 "/", "_", 100 "|", "_", 101 "\\", "_", 102 ":", "_", 103 ";", "_", 104 ">", "_", 105 "=", "_", 106 "<", "_", 107 "?", "_", 108 "[", "_", 109 "]", "_", 110 "^", "_", 111 "`", "_", 112 "~", "_", 113 "$", "_", 114 "@", "_at_") 115 replaced := r.Replace(strings.ToLower(s)) 116 117 result, err := normalize.String(replaced) 118 if err != nil { 119 return replaced 120 } 121 122 return result 123 } 124 125 type getListWithoutArg interface { 126 GetList() ([]string, error) 127 } 128 129 type getListWithOneArg interface { 130 GetList(string) ([]string, error) 131 } 132 133 type getListWithTwoArgs interface { 134 GetList(string, string) ([]string, error) 135 } 136 137 type getListWithThreeArgs interface { 138 GetList(string, string, string) ([]string, error) 139 } 140 141 type getListWithFourArgs interface { 142 GetList(string, string, string, string) ([]string, error) 143 } 144 145 type getListWithFiveArgs interface { 146 GetList(string, string, string, string, string) ([]string, error) 147 } 148 149 type getGeneric struct { 150 i interface{} 151 params []string 152 } 153 154 func contains(s []string, e string) bool { 155 for _, v := range s { 156 if v == e { 157 return true 158 } 159 } 160 return false 161 }