|
| 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 | +) |
| 9 | + |
| 10 | +func dataSourceManagementAdministrator() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Read: dataSourceManagementAdministratorRead, |
| 13 | + Schema: map[string]*schema.Schema{ |
| 14 | + "uid": { |
| 15 | + Type: schema.TypeString, |
| 16 | + Optional: true, |
| 17 | + Description: "Object unique identifier.", |
| 18 | + }, |
| 19 | + "name": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Optional: true, |
| 22 | + Description: "Object name.", |
| 23 | + }, |
| 24 | + "authentication_method": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Computed: true, |
| 27 | + Description: "Authentication method.", |
| 28 | + }, |
| 29 | + "email": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Computed: true, |
| 32 | + Description: "Administrator email.", |
| 33 | + }, |
| 34 | + "expiration_date": { |
| 35 | + Type: schema.TypeMap, |
| 36 | + Computed: true, |
| 37 | + Elem: &schema.Resource{ |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "iso_8601": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Description: "Date and time represented in international ISO 8601 format.", |
| 43 | + }, |
| 44 | + "posix": { |
| 45 | + Type: schema.TypeInt, |
| 46 | + Computed: true, |
| 47 | + Description: "Number of milliseconds that have elapsed since 00:00:00, 1 January 1970.", |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + "multi_domain_profile": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + Description: "Administrator multi-domain profile. Level of details in the output corresponds to the number of details for search. This table shows the level of details in the Standard level.", |
| 56 | + }, |
| 57 | + "must_change_password": { |
| 58 | + Type: schema.TypeBool, |
| 59 | + Computed: true, |
| 60 | + Description: "True if administrator must change password on the next login.", |
| 61 | + }, |
| 62 | + "permissions_profile": { |
| 63 | + Type: schema.TypeList, |
| 64 | + Optional: true, |
| 65 | + Description: "Administrator permissions profile. Permissions profile should not be provided when multi-domain-profile is set to \"Multi-Domain Super User\" or \"Domain Super User\".", |
| 66 | + Elem: &schema.Resource{ |
| 67 | + Schema: map[string]*schema.Schema{ |
| 68 | + "domain": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Required: true, |
| 71 | + }, |
| 72 | + "profile": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Required: true, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + "phone_number": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Computed: true, |
| 82 | + Description: "Administrator phone number.", |
| 83 | + }, |
| 84 | + "radius_server": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + Description: "RADIUS server object identified by the name or UID. Must be set when \"authentication-method\" was selected to be \"RADIUS\". Level of details in the output corresponds to the number of details for search. This table shows the level of details in the Standard level.", |
| 88 | + }, |
| 89 | + "sic_name": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Computed: true, |
| 92 | + Description: "Name of the Secure Internal Connection Trust.", |
| 93 | + }, |
| 94 | + "tacacs_server": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + Description: "TACACS server object identified by the name or UID . Must be set when \"authentication-method\" was selected to be \"TACACS\". Level of details in the output corresponds to the number of details for search. This table shows the level of details in the Standard level.", |
| 98 | + }, |
| 99 | + "tags": { |
| 100 | + Type: schema.TypeSet, |
| 101 | + Computed: true, |
| 102 | + Description: "Collection of tag identifiers.", |
| 103 | + Elem: &schema.Schema{ |
| 104 | + Type: schema.TypeString, |
| 105 | + }, |
| 106 | + }, |
| 107 | + "color": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Computed: true, |
| 110 | + Description: "Color of the object. Should be one of existing colors.", |
| 111 | + }, |
| 112 | + "comments": { |
| 113 | + Type: schema.TypeString, |
| 114 | + Computed: true, |
| 115 | + Description: "Comments string.", |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func dataSourceManagementAdministratorRead(d *schema.ResourceData, m interface{}) error { |
| 122 | + client := m.(*checkpoint.ApiClient) |
| 123 | + |
| 124 | + name := d.Get("name").(string) |
| 125 | + uid := d.Get("uid").(string) |
| 126 | + |
| 127 | + payload := make(map[string]interface{}) |
| 128 | + |
| 129 | + if name != "" { |
| 130 | + payload["name"] = name |
| 131 | + } else if uid != "" { |
| 132 | + payload["uid"] = uid |
| 133 | + } |
| 134 | + |
| 135 | + showAdministratorRes, err := client.ApiCall("show-administrator", payload, client.GetSessionID(), true, client.IsProxyUsed()) |
| 136 | + if err != nil { |
| 137 | + return fmt.Errorf(err.Error()) |
| 138 | + } |
| 139 | + if !showAdministratorRes.Success { |
| 140 | + return fmt.Errorf(showAdministratorRes.ErrorMsg) |
| 141 | + } |
| 142 | + |
| 143 | + administrator := showAdministratorRes.GetData() |
| 144 | + log.Println("Read Administrator - Show JSON = ", administrator) |
| 145 | + |
| 146 | + if v := administrator["name"]; v != nil { |
| 147 | + _ = d.Set("name", v) |
| 148 | + } |
| 149 | + |
| 150 | + if v := administrator["uid"]; v != nil { |
| 151 | + _ = d.Set("uid", v) |
| 152 | + d.SetId(v.(string)) |
| 153 | + } |
| 154 | + |
| 155 | + if v := administrator["authentication-method"]; v != nil { |
| 156 | + _ = d.Set("authentication_method", v) |
| 157 | + } |
| 158 | + |
| 159 | + if v := administrator["email"]; v != nil { |
| 160 | + _ = d.Set("email", v) |
| 161 | + } |
| 162 | + |
| 163 | + if v := administrator["expiration-date"]; v != nil { |
| 164 | + _ = d.Set("expiration_date", v) |
| 165 | + } |
| 166 | + |
| 167 | + if administrator["multi-domain-profile"] != nil { |
| 168 | + if multiDomainProfileMap, ok := administrator["multi-domain-profile"].(map[string]interface{}); ok { |
| 169 | + if v, _ := multiDomainProfileMap["name"]; v != nil { |
| 170 | + _ = d.Set("multi_domain_profile", v) |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if v := administrator["must-change-password"]; v != nil { |
| 176 | + _ = d.Set("must_change_password", v) |
| 177 | + } |
| 178 | + |
| 179 | + if v := administrator["password"]; v != nil { |
| 180 | + _ = d.Set("password", v) |
| 181 | + } |
| 182 | + |
| 183 | + if v := administrator["password-hash"]; v != nil { |
| 184 | + _ = d.Set("password_hash", v) |
| 185 | + } |
| 186 | + |
| 187 | + if v := administrator["must-change-password"]; v != nil { |
| 188 | + _ = d.Set("must_change_password", v) |
| 189 | + } |
| 190 | + |
| 191 | + if administrator["permissions-profile"] != nil { |
| 192 | + var permissionsProfileListToReturn []map[string]interface{} |
| 193 | + |
| 194 | + if permissionsProfileList, ok := administrator["permissions-profile"].([]interface{}); ok { |
| 195 | + |
| 196 | + for i := range permissionsProfileList { |
| 197 | + permissionsProfileMap := permissionsProfileList[i].(map[string]interface{}) |
| 198 | + |
| 199 | + permissionsProfileMapToAdd := make(map[string]interface{}) |
| 200 | + |
| 201 | + if profile, _ := permissionsProfileMap["profile"]; profile != nil { |
| 202 | + if v, _ := profile.(map[string]interface{})["name"]; v != nil { |
| 203 | + permissionsProfileMapToAdd["profile"] = v.(string) |
| 204 | + } |
| 205 | + } |
| 206 | + if domain, _ := permissionsProfileMap["domain"]; domain != nil { |
| 207 | + if v, _ := domain.(map[string]interface{})["name"]; v != nil { |
| 208 | + permissionsProfileMapToAdd["domain"] = v.(string) |
| 209 | + } |
| 210 | + } |
| 211 | + permissionsProfileListToReturn = append(permissionsProfileListToReturn, permissionsProfileMapToAdd) |
| 212 | + } |
| 213 | + |
| 214 | + } else if v, ok := administrator["permissions-profile"].(map[string]interface{}); ok { |
| 215 | + permissionsProfileListToReturn = []map[string]interface{}{ |
| 216 | + { |
| 217 | + "domain": "SMC User", |
| 218 | + "profile": v["name"].(string), |
| 219 | + }, |
| 220 | + } |
| 221 | + } |
| 222 | + _ = d.Set("permissions_profile", permissionsProfileListToReturn) |
| 223 | + |
| 224 | + } |
| 225 | + |
| 226 | + if v := administrator["phone-number"]; v != nil { |
| 227 | + _ = d.Set("phone_number", v) |
| 228 | + } |
| 229 | + |
| 230 | + if v := administrator["radius-server"]; v != nil { |
| 231 | + _ = d.Set("radius_server", v) |
| 232 | + } |
| 233 | + |
| 234 | + if v := administrator["tacacs-server"]; v != nil { |
| 235 | + _ = d.Set("tacacs_server", v) |
| 236 | + } |
| 237 | + |
| 238 | + if administrator["tags"] != nil { |
| 239 | + tagsJson := administrator["tags"].([]interface{}) |
| 240 | + var tagsIds = make([]string, 0) |
| 241 | + if len(tagsJson) > 0 { |
| 242 | + // Create slice of tag names |
| 243 | + for _, tag := range tagsJson { |
| 244 | + tag := tag.(map[string]interface{}) |
| 245 | + tagsIds = append(tagsIds, tag["name"].(string)) |
| 246 | + } |
| 247 | + } |
| 248 | + _ = d.Set("tags", tagsIds) |
| 249 | + } else { |
| 250 | + _ = d.Set("tags", nil) |
| 251 | + } |
| 252 | + |
| 253 | + if v := administrator["color"]; v != nil { |
| 254 | + _ = d.Set("color", v) |
| 255 | + } |
| 256 | + |
| 257 | + if v := administrator["comments"]; v != nil { |
| 258 | + _ = d.Set("comments", v) |
| 259 | + } |
| 260 | + |
| 261 | + if v := administrator["sic-name"]; v != nil { |
| 262 | + _ = d.Set("sic_name", v) |
| 263 | + } |
| 264 | + |
| 265 | + return nil |
| 266 | +} |
0 commit comments