|
| 1 | +package checkpoint |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + checkpoint "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 7 | + "log" |
| 8 | + "strconv" |
| 9 | +) |
| 10 | + |
| 11 | +func dataSourceManagementClusterMember() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Read: dataSourceManagementClusterMemberRead, |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "uid": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Required: true, |
| 18 | + Description: "Cluster member unique identifier.", |
| 19 | + }, |
| 20 | + "limit_interfaces": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Optional: true, |
| 23 | + Description: "Limit number of cluster member interfaces to show.", |
| 24 | + Default: 50, |
| 25 | + }, |
| 26 | + "name": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Computed: true, |
| 29 | + Description: "N/A", |
| 30 | + }, |
| 31 | + "cluster_uid": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + Description: "Cluster object (the owner of this member) uid.", |
| 35 | + }, |
| 36 | + "interfaces": { |
| 37 | + Type: schema.TypeList, |
| 38 | + Computed: true, |
| 39 | + Description: "Cluster member network interfaces.", |
| 40 | + Elem: &schema.Resource{ |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "ipv4_address": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Computed: true, |
| 45 | + Description: "IPv4 address.", |
| 46 | + }, |
| 47 | + "ipv4_mask_length": { |
| 48 | + Type: schema.TypeInt, |
| 49 | + Computed: true, |
| 50 | + Description: "IPv4 network mask length.", |
| 51 | + }, |
| 52 | + "ipv4_network_mask": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: "IPv4 network mask.", |
| 56 | + }, |
| 57 | + "name": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + Description: "Cluster member interface name.", |
| 61 | + }, |
| 62 | + "ipv6_address": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + Description: "IPv6 address.", |
| 66 | + }, |
| 67 | + "ipv6_mask_length": { |
| 68 | + Type: schema.TypeInt, |
| 69 | + Computed: true, |
| 70 | + Description: "IPv6 network mask length.", |
| 71 | + }, |
| 72 | + "ipv6_network_mask": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "IPv6 network mask.", |
| 76 | + }, |
| 77 | + "uid": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "Cluster member interface object UID.", |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + "ip_address": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Computed: true, |
| 88 | + Description: "Cluster member IP address.", |
| 89 | + }, |
| 90 | + "ipv6_address": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Computed: true, |
| 93 | + Description: "Cluster member IPv6 address.", |
| 94 | + }, |
| 95 | + "sic_message": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + Description: "Secure Internal Communication message.", |
| 99 | + }, |
| 100 | + "sic_state": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Computed: true, |
| 103 | + Description: "Secure Internal Communication state.", |
| 104 | + }, |
| 105 | + "type": { |
| 106 | + Type: schema.TypeString, |
| 107 | + Computed: true, |
| 108 | + Description: "Object type.", |
| 109 | + }, |
| 110 | + "priority": { |
| 111 | + Type: schema.TypeInt, |
| 112 | + Computed: true, |
| 113 | + Description: "In a High Availability New mode cluster each machine is given a priority. The highest priority machine serves as the gateway in normal circumstances. If this machine fails, control is passed to the next highest priority machine. If that machine fails, control is passed to the next machine, and so on. In Load Sharing Unicast mode cluster, the highest priority is the pivot machine. The values must be in a range from 1 to N, where N is number of cluster members.", |
| 114 | + }, |
| 115 | + "nat_settings": { |
| 116 | + Type: schema.TypeMap, |
| 117 | + Computed: true, |
| 118 | + Description: "NAT settings.", |
| 119 | + Elem: &schema.Resource{ |
| 120 | + Schema: map[string]*schema.Schema{ |
| 121 | + "auto_rule": { |
| 122 | + Type: schema.TypeBool, |
| 123 | + Optional: true, |
| 124 | + Description: "Whether to add automatic address translation rules.", |
| 125 | + }, |
| 126 | + "hide_behind": { |
| 127 | + Type: schema.TypeString, |
| 128 | + Optional: true, |
| 129 | + Description: "Hide behind method. This parameter is forbidden in case \"method\" parameter is \"static\".", |
| 130 | + }, |
| 131 | + "install_on": { |
| 132 | + Type: schema.TypeString, |
| 133 | + Optional: true, |
| 134 | + Description: "Which gateway should apply the NAT translation.", |
| 135 | + }, |
| 136 | + "ipv4_address": { |
| 137 | + Type: schema.TypeString, |
| 138 | + Optional: true, |
| 139 | + Description: "IPv4 address.", |
| 140 | + }, |
| 141 | + "ipv6_address": { |
| 142 | + Type: schema.TypeString, |
| 143 | + Optional: true, |
| 144 | + Description: "IPv6 address.", |
| 145 | + }, |
| 146 | + "method": { |
| 147 | + Type: schema.TypeString, |
| 148 | + Optional: true, |
| 149 | + Description: "NAT translation method.", |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func dataSourceManagementClusterMemberRead(d *schema.ResourceData, m interface{}) error { |
| 159 | + |
| 160 | + client := m.(*checkpoint.ApiClient) |
| 161 | + |
| 162 | + uid := d.Get("uid").(string) |
| 163 | + d.SetId(uid) |
| 164 | + |
| 165 | + payload := make(map[string]interface{}) |
| 166 | + |
| 167 | + payload["uid"] = uid |
| 168 | + |
| 169 | + if v, ok := d.GetOk("limit_interfaces"); ok { |
| 170 | + payload["limit-interfaces"] = v |
| 171 | + } |
| 172 | + |
| 173 | + showClusterMemberRes, err := client.ApiCall("show-cluster-member", payload, client.GetSessionID(), true, client.IsProxyUsed()) |
| 174 | + if err != nil { |
| 175 | + return fmt.Errorf(err.Error()) |
| 176 | + } |
| 177 | + if !showClusterMemberRes.Success { |
| 178 | + if objectNotFound(showClusterMemberRes.GetData()["code"].(string)) { |
| 179 | + d.SetId("") |
| 180 | + return nil |
| 181 | + } |
| 182 | + return fmt.Errorf(showClusterMemberRes.ErrorMsg) |
| 183 | + } |
| 184 | + |
| 185 | + clusterMember := showClusterMemberRes.GetData() |
| 186 | + |
| 187 | + log.Println("Read ClusterMember - Show JSON = ", clusterMember) |
| 188 | + |
| 189 | + if v := clusterMember["name"]; v != nil { |
| 190 | + _ = d.Set("name", v) |
| 191 | + } |
| 192 | + |
| 193 | + if v := clusterMember["type"]; v != nil { |
| 194 | + _ = d.Set("type", v) |
| 195 | + } |
| 196 | + |
| 197 | + if v := clusterMember["cluster-uid"]; v != nil { |
| 198 | + _ = d.Set("cluster_uid", v) |
| 199 | + } |
| 200 | + |
| 201 | + if v := clusterMember["ip-address"]; v != nil { |
| 202 | + _ = d.Set("ip_address", v) |
| 203 | + } |
| 204 | + |
| 205 | + if v := clusterMember["ipv6-address"]; v != nil { |
| 206 | + _ = d.Set("ipv6_address", v) |
| 207 | + } |
| 208 | + |
| 209 | + if v := clusterMember["sic-message"]; v != nil { |
| 210 | + _ = d.Set("sic_message", v) |
| 211 | + } |
| 212 | + |
| 213 | + if v := clusterMember["sic-state"]; v != nil { |
| 214 | + _ = d.Set("sic_state", v) |
| 215 | + } |
| 216 | + |
| 217 | + if v := clusterMember["priority"]; v != nil { |
| 218 | + _ = d.Set("priority", v.(int)) |
| 219 | + } |
| 220 | + |
| 221 | + if clusterMember["interfaces"] != nil { |
| 222 | + |
| 223 | + interfacesList, ok := clusterMember["interfaces"].([]interface{}) |
| 224 | + |
| 225 | + var interfacesListToReturn []map[string]interface{} |
| 226 | + |
| 227 | + if ok { |
| 228 | + |
| 229 | + if len(interfacesList) > 0 { |
| 230 | + |
| 231 | + for i := range interfacesList { |
| 232 | + |
| 233 | + interfacesMap := interfacesList[i].(map[string]interface{}) |
| 234 | + |
| 235 | + interfacesMapToAdd := make(map[string]interface{}) |
| 236 | + |
| 237 | + if v, _ := interfacesMap["ipv4-address"]; v != nil { |
| 238 | + interfacesMapToAdd["ipv4_address"] = v |
| 239 | + } |
| 240 | + if v, _ := interfacesMap["ipv4-mask-length"]; v != nil { |
| 241 | + interfacesMapToAdd["ipv4_mask_length"] = v |
| 242 | + } |
| 243 | + if v, _ := interfacesMap["ipv4-network-mask"]; v != nil { |
| 244 | + interfacesMapToAdd["ipv4_network_mask"] = v |
| 245 | + } |
| 246 | + if v, _ := interfacesMap["name"]; v != nil { |
| 247 | + interfacesMapToAdd["name"] = v |
| 248 | + } |
| 249 | + if v, _ := interfacesMap["ipv6-address"]; v != nil { |
| 250 | + interfacesMapToAdd["ipv^_address"] = v |
| 251 | + } |
| 252 | + if v, _ := interfacesMap["ipv6-mask-length"]; v != nil { |
| 253 | + interfacesMapToAdd["ipv6_mask_length"] = v |
| 254 | + } |
| 255 | + if v, _ := interfacesMap["ipv6-network-mask"]; v != nil { |
| 256 | + interfacesMapToAdd["ipv6_network_mask"] = v |
| 257 | + } |
| 258 | + if v, _ := interfacesMap["uid"]; v != nil { |
| 259 | + interfacesMapToAdd["uid"] = v |
| 260 | + } |
| 261 | + interfacesListToReturn = append(interfacesListToReturn, interfacesMapToAdd) |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + _ = d.Set("interfaces", interfacesListToReturn) |
| 266 | + } |
| 267 | + |
| 268 | + if clusterMember["nat-settings"] != nil { |
| 269 | + |
| 270 | + actionSettingsMap := clusterMember["nat-settings"].(map[string]interface{}) |
| 271 | + |
| 272 | + actionSettingsMapToReturn := make(map[string]interface{}) |
| 273 | + |
| 274 | + if v, _ := actionSettingsMap["auto-rule"]; v != nil { |
| 275 | + actionSettingsMapToReturn["auto_rule"] = strconv.FormatBool(v.(bool)) |
| 276 | + } |
| 277 | + |
| 278 | + if v, _ := actionSettingsMap["hide-behind"]; v != nil { |
| 279 | + actionSettingsMapToReturn["hide_behind"] = v |
| 280 | + } |
| 281 | + |
| 282 | + if v, _ := actionSettingsMap["install-on"]; v != nil { |
| 283 | + actionSettingsMapToReturn["install_on"] = v |
| 284 | + } |
| 285 | + |
| 286 | + if v, _ := actionSettingsMap["ipv4-address"]; v != nil { |
| 287 | + actionSettingsMapToReturn["ipv4_address"] = v |
| 288 | + } |
| 289 | + |
| 290 | + if v, _ := actionSettingsMap["ipv6-address"]; v != nil { |
| 291 | + actionSettingsMapToReturn["ipv6_address"] = v |
| 292 | + } |
| 293 | + |
| 294 | + if v, _ := actionSettingsMap["method"]; v != nil { |
| 295 | + actionSettingsMapToReturn["method"] = v |
| 296 | + } |
| 297 | + |
| 298 | + _ = d.Set("nat_settings", actionSettingsMapToReturn) |
| 299 | + } else { |
| 300 | + _ = d.Set("nat_settings", nil) |
| 301 | + } |
| 302 | + |
| 303 | + return nil |
| 304 | + |
| 305 | +} |
0 commit comments