github.com/polarismesh/polaris@v1.17.8/plugin/cmdb/memory/model.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package memory 19 20 import ( 21 "net" 22 "strings" 23 24 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 25 "google.golang.org/protobuf/types/known/wrapperspb" 26 27 "github.com/polarismesh/polaris/common/model" 28 ) 29 30 // IPType ip type 31 type IPType string 32 33 const ( 34 Host IPType = "host" 35 Mask IPType = "mask" 36 Backoff IPType = "backoff" 37 ) 38 39 // Request request cmdb data 40 type Request struct { 41 RequestID string `json:"request_id"` 42 PageNo int64 `json:"page_no"` 43 PageSize int64 `json:"page_size"` 44 } 45 46 // Response response cmdb data 47 type Response struct { 48 Total int `json:"total"` 49 Size int `json:"size"` 50 Code int `json:"code"` 51 Info string `json:"info"` 52 Priority string `json:"priority"` 53 Data []IPInfo `json:"data"` 54 } 55 56 // IPInfo ip info 57 type IPInfo struct { 58 IP string `json:"ip"` 59 Type IPType `json:"type"` 60 Region LocationInfo `json:"region"` 61 Zone LocationInfo `json:"zone"` 62 Campus LocationInfo `json:"campus"` 63 } 64 65 // IP ip info 66 type IP struct { 67 IP string 68 Type IPType 69 ipNet *net.IPNet 70 loc *model.Location 71 } 72 73 func NewIP(info IPInfo) (IP, error) { 74 var ip IP 75 76 if info.Type == Mask { 77 _, cidr, err := net.ParseCIDR(info.IP) 78 if err != nil { 79 return IP{}, err 80 } 81 82 ip.ipNet = cidr 83 } 84 85 regionId, _ := info.Region.Metadata["id"].(int64) 86 zoneId, _ := info.Zone.Metadata["id"].(int64) 87 campusId, _ := info.Campus.Metadata["id"].(int64) 88 89 ip.IP = info.IP 90 ip.Type = info.Type 91 ip.loc = &model.Location{ 92 Proto: &apimodel.Location{ 93 Region: &wrapperspb.StringValue{ 94 Value: info.Region.Name, 95 }, 96 Zone: &wrapperspb.StringValue{ 97 Value: info.Zone.Name, 98 }, 99 Campus: &wrapperspb.StringValue{ 100 Value: info.Campus.Name, 101 }, 102 }, 103 RegionID: uint32(regionId), 104 ZoneID: uint32(zoneId), 105 CampusID: uint32(campusId), 106 Valid: false, 107 } 108 109 return ip, nil 110 } 111 112 // Match target ip is match 113 func (p IP) Match(ip string) bool { 114 switch p.Type { 115 case Host: 116 return strings.Compare(p.IP, ip) == 0 117 case Mask: 118 return p.ipNet.Contains(net.ParseIP(ip)) 119 case Backoff: 120 return true 121 default: 122 return false 123 } 124 } 125 126 // LocationInfo 127 type LocationInfo struct { 128 Name string `json:"name"` 129 Metadata map[string]interface{} `json:"metadata"` 130 }