|
| 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 dataSourceManagementGaiaBestPractice() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Read: dataSourceManagementGaiaBestPracticeRead, |
| 13 | + Schema: map[string]*schema.Schema{ |
| 14 | + "best_practice_id": { |
| 15 | + Type: schema.TypeString, |
| 16 | + Optional: true, |
| 17 | + Description: "Best Practice ID.", |
| 18 | + }, |
| 19 | + "name": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Optional: true, |
| 22 | + Description: "Best Practice Name.", |
| 23 | + }, |
| 24 | + "uid": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Optional: true, |
| 27 | + Description: "Best Practice UID.", |
| 28 | + }, |
| 29 | + "action_item": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Computed: true, |
| 32 | + Description: "Action item to comply with the Best Practice.", |
| 33 | + }, |
| 34 | + "description": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Computed: true, |
| 37 | + Description: "Description of the Best Practice.", |
| 38 | + }, |
| 39 | + "expected_output_base64": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Description: "The expected output of the script in Base64. Available only for user-defined best practices.", |
| 43 | + }, |
| 44 | + "practice_script_base64": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + Description: "The script to run on Gaia Security Gateways during the Compliance scans in Base64. Available only for user-defined best practices.", |
| 48 | + }, |
| 49 | + "regulations": { |
| 50 | + Type: schema.TypeList, |
| 51 | + Computed: true, |
| 52 | + Description: "The applicable regulations of the Gaia Best Practice. Appear only when the value of the 'details-level' parameter is set to 'full'.", |
| 53 | + Elem: &schema.Resource{ |
| 54 | + Schema: map[string]*schema.Schema{ |
| 55 | + "regulation_name": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "The name of the regulation.", |
| 59 | + }, |
| 60 | + "requirement_description": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "The description of the requirement.", |
| 64 | + }, |
| 65 | + "requirement_id": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + Description: "The id of the requirement.", |
| 69 | + }, |
| 70 | + "requirement_status": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "The status of the requirement.", |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + "relevant_objects": { |
| 79 | + Type: schema.TypeList, |
| 80 | + Computed: true, |
| 81 | + Description: "The applicable objects of the Gaia Best Practice. Appear only when the value of the 'details-level' parameter is set to 'full'.", |
| 82 | + Elem: &schema.Resource{ |
| 83 | + Schema: map[string]*schema.Schema{ |
| 84 | + "enabled": { |
| 85 | + Type: schema.TypeBool, |
| 86 | + Computed: true, |
| 87 | + Description: "Determines if the relevant object is enabled or not.", |
| 88 | + }, |
| 89 | + "name": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Computed: true, |
| 92 | + Description: "The name of the relevant object.", |
| 93 | + }, |
| 94 | + "status": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + Description: "The status of the relevant object.", |
| 98 | + }, |
| 99 | + "uid": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + Description: "The uid of the relevant object.", |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + "status": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Computed: true, |
| 110 | + Description: "The current status of the Best Practice.", |
| 111 | + }, |
| 112 | + "user_defined": { |
| 113 | + Type: schema.TypeBool, |
| 114 | + Computed: true, |
| 115 | + Description: "Determines if the Gaia Best Practice is a user-defined best practice.", |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func dataSourceManagementGaiaBestPracticeRead(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 | + bestPracticeId := d.Get("best_practice_id").(string) |
| 127 | + |
| 128 | + payload := make(map[string]interface{}) |
| 129 | + |
| 130 | + if name != "" { |
| 131 | + payload["name"] = name |
| 132 | + } else if uid != "" { |
| 133 | + payload["uid"] = uid |
| 134 | + } else if bestPracticeId != "" { |
| 135 | + payload["best-practice-id"] = bestPracticeId |
| 136 | + } |
| 137 | + |
| 138 | + showGaiaBestPractice, err := client.ApiCall("show-gaia-best-practice", payload, client.GetSessionID(), true, client.IsProxyUsed()) |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf(err.Error()) |
| 141 | + } |
| 142 | + if !showGaiaBestPractice.Success { |
| 143 | + return fmt.Errorf(showGaiaBestPractice.ErrorMsg) |
| 144 | + } |
| 145 | + |
| 146 | + gaiaBestPractice := showGaiaBestPractice.GetData() |
| 147 | + |
| 148 | + log.Println("Read Gaia Best Practice - Show JSON = ", gaiaBestPractice) |
| 149 | + |
| 150 | + if v := gaiaBestPractice["uid"]; v != nil { |
| 151 | + _ = d.Set("uid", v) |
| 152 | + d.SetId(v.(string)) |
| 153 | + } |
| 154 | + |
| 155 | + if v := gaiaBestPractice["name"]; v != nil { |
| 156 | + _ = d.Set("name", v) |
| 157 | + } |
| 158 | + |
| 159 | + if v := gaiaBestPractice["best-practice-id"]; v != nil { |
| 160 | + _ = d.Set("best_practice_id", v) |
| 161 | + } |
| 162 | + |
| 163 | + if v := gaiaBestPractice["action-item"]; v != nil { |
| 164 | + _ = d.Set("action_item", v) |
| 165 | + } |
| 166 | + |
| 167 | + if v := gaiaBestPractice["description"]; v != nil { |
| 168 | + _ = d.Set("description", v) |
| 169 | + } |
| 170 | + |
| 171 | + if v := gaiaBestPractice["expected-output-base64"]; v != nil { |
| 172 | + _ = d.Set("expected_output_base64", v) |
| 173 | + } |
| 174 | + |
| 175 | + if v := gaiaBestPractice["practice-script-base64"]; v != nil { |
| 176 | + _ = d.Set("practice_script_base64", v) |
| 177 | + } |
| 178 | + |
| 179 | + if gaiaBestPractice["regulations"] != nil { |
| 180 | + regulationsList := gaiaBestPractice["regulations"].([]interface{}) |
| 181 | + |
| 182 | + if len(regulationsList) > 0 { |
| 183 | + var regulationsListToReturn []map[string]interface{} |
| 184 | + |
| 185 | + for i := range regulationsList { |
| 186 | + regulationsMap := regulationsList[i].(map[string]interface{}) |
| 187 | + |
| 188 | + regulationsMapToAdd := make(map[string]interface{}) |
| 189 | + |
| 190 | + if v, _ := regulationsMap["regulation-name"]; v != nil { |
| 191 | + regulationsMapToAdd["regulation_name"] = v |
| 192 | + } |
| 193 | + if v, _ := regulationsMap["regulation-description"]; v != nil { |
| 194 | + regulationsMapToAdd["regulation_description"] = v |
| 195 | + } |
| 196 | + if v, _ := regulationsMap["requirement-id"]; v != nil { |
| 197 | + regulationsMapToAdd["requirement_id"] = v |
| 198 | + } |
| 199 | + if v, _ := regulationsMap["requirement-status"]; v != nil { |
| 200 | + regulationsMapToAdd["requirement_status"] = v |
| 201 | + } |
| 202 | + |
| 203 | + regulationsListToReturn = append(regulationsListToReturn, regulationsMapToAdd) |
| 204 | + } |
| 205 | + |
| 206 | + _ = d.Set("regulations", regulationsListToReturn) |
| 207 | + } else { |
| 208 | + _ = d.Set("regulations", regulationsList) |
| 209 | + } |
| 210 | + } else { |
| 211 | + _ = d.Set("regulations", nil) |
| 212 | + } |
| 213 | + |
| 214 | + if gaiaBestPractice["relevant-objects"] != nil { |
| 215 | + relevantObjectsList := gaiaBestPractice["relevant-objects"].([]interface{}) |
| 216 | + |
| 217 | + if len(relevantObjectsList) > 0 { |
| 218 | + var relevantObjectsListToReturn []map[string]interface{} |
| 219 | + |
| 220 | + for i := range relevantObjectsList { |
| 221 | + relevantObjectsMap := relevantObjectsList[i].(map[string]interface{}) |
| 222 | + |
| 223 | + relevantObjectsMapToAdd := make(map[string]interface{}) |
| 224 | + |
| 225 | + if v, _ := relevantObjectsMap["enabled"]; v != nil { |
| 226 | + relevantObjectsMapToAdd["enabled"] = v |
| 227 | + } |
| 228 | + if v, _ := relevantObjectsMap["name"]; v != nil { |
| 229 | + relevantObjectsMapToAdd["name"] = v |
| 230 | + } |
| 231 | + if v, _ := relevantObjectsMap["status"]; v != nil { |
| 232 | + relevantObjectsMapToAdd["status"] = v |
| 233 | + } |
| 234 | + if v, _ := relevantObjectsMap["uid"]; v != nil { |
| 235 | + relevantObjectsMapToAdd["uid"] = v |
| 236 | + } |
| 237 | + |
| 238 | + relevantObjectsListToReturn = append(relevantObjectsListToReturn, relevantObjectsMapToAdd) |
| 239 | + } |
| 240 | + |
| 241 | + _ = d.Set("relevant_objects", relevantObjectsListToReturn) |
| 242 | + } else { |
| 243 | + _ = d.Set("relevant_objects", relevantObjectsList) |
| 244 | + } |
| 245 | + } else { |
| 246 | + _ = d.Set("relevant_objects", nil) |
| 247 | + } |
| 248 | + |
| 249 | + if v := gaiaBestPractice["status"]; v != nil { |
| 250 | + _ = d.Set("status", v) |
| 251 | + } |
| 252 | + |
| 253 | + if v := gaiaBestPractice["user-defined"]; v != nil { |
| 254 | + _ = d.Set("user_defined", v) |
| 255 | + } |
| 256 | + |
| 257 | + return nil |
| 258 | +} |
0 commit comments