-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
204 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package checkpoint | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
checkpoint "github.com/CheckPointSW/cp-mgmt-api-go-sdk/APIFiles" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func resourceManagementGenericApi() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: createManagementGenericApi, | ||
Read: readManagementGenericApi, | ||
Delete: deleteManagementGenericApi, | ||
Schema: map[string]*schema.Schema{ | ||
"api_command": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "API command name or path", | ||
}, | ||
"payload": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Description: "Request payload in JSON format", | ||
}, | ||
"method": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Description: "HTTP request method", | ||
Default: "POST", | ||
}, | ||
"response": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Response message in JSON format", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func createManagementGenericApi(d *schema.ResourceData, m interface{}) error { | ||
client := m.(*checkpoint.ApiClient) | ||
|
||
apiCommand := d.Get("api_command").(string) | ||
|
||
// Convert payload from string to map | ||
var payload = map[string]interface{}{} | ||
if v, ok := d.GetOk("payload"); ok { | ||
err := json.Unmarshal([]byte(v.(string)), &payload) | ||
if err != nil { | ||
return fmt.Errorf(err.Error()) | ||
} | ||
} | ||
|
||
var method string | ||
if v, ok := d.GetOk("method"); ok { | ||
method = v.(string) | ||
} | ||
|
||
genericApiRes, err := client.ApiCall(apiCommand, payload, client.GetSessionID(), true, client.IsProxyUsed(), method) | ||
if err != nil { | ||
return fmt.Errorf(err.Error()) | ||
} | ||
if !genericApiRes.Success { | ||
return fmt.Errorf(genericApiRes.ErrorMsg) | ||
} | ||
|
||
// Convert response from map to string | ||
jsonResponse, err := json.Marshal(genericApiRes.GetData()) | ||
if err != nil { | ||
return fmt.Errorf(err.Error()) | ||
} | ||
if jsonResponse != nil { | ||
_ = d.Set("response", string(jsonResponse)) | ||
} | ||
|
||
d.SetId("generic-api-" + apiCommand + "-" + acctest.RandString(10)) | ||
|
||
return readManagementGaiaApi(d, m) | ||
} | ||
|
||
func readManagementGenericApi(d *schema.ResourceData, m interface{}) error { | ||
return nil | ||
} | ||
|
||
func deleteManagementGenericApi(d *schema.ResourceData, m interface{}) error { | ||
d.SetId("") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
website/docs/r/checkpoint_management_generic_api.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
layout: "checkpoint" | ||
page_title: "checkpoint_generic_api" | ||
sidebar_current: "docs-checkpoint-resource-checkpoint-generic-api" | ||
description: |- | ||
This resource allows you to execute generic Management API calls. | ||
--- | ||
|
||
# Resource: checkpoint_generic_api | ||
|
||
This resource allows you to execute Check Point generic Management or GAIA API.<br> | ||
See the [Management API reference](https://sc1.checkpoint.com/documents/latest/APIs/index.html) or [GAIA API reference](https://sc1.checkpoint.com/documents/latest/GaiaAPIs/index.html) for a complete list of APIs you can run on your Check Point server.<br> | ||
<b>NOTE:</b> If you configure the provider [context](https://registry.terraform.io/providers/CheckPointSW/checkpoint/latest/docs#context-1) to `gaia_api` you can execute only GAIA API and GAIA resources. Management API or any other resources will not be supported. | ||
|
||
## Example Usage | ||
|
||
|
||
```hcl | ||
# Run generic Management API when provider context is 'web_api' | ||
resource "checkpoint_generic_api" "api1" { | ||
api_command = "add-host" | ||
payload = <<EOT | ||
{ | ||
"name": "host1", | ||
"ip-address": "1.2.3.4" | ||
} | ||
EOT | ||
} | ||
# Run generic Management API when provider context is 'web_api' | ||
resource "checkpoint_generic_api" "api2" { | ||
api_command = "show-hosts" | ||
} | ||
# Run generic Management API when provider context is 'web_api' | ||
resource "checkpoint_generic_api" "api3" { | ||
api_command = "gaia-api/show-proxy" | ||
payload = <<EOT | ||
{ | ||
"target": "gateway1", | ||
} | ||
EOT | ||
} | ||
# Run generic GAIA API when provider context is 'gaia_api' | ||
resource "checkpoint_generic_api" "api4" { | ||
api_command = "show-proxy" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `api_command` - (Required) API command name or path. | ||
* `payload` - (Optional) Request payload in JSON format. You can use [heredoc strings](https://developer.hashicorp.com/terraform/language/expressions/strings#heredoc-strings) to write freestyle JSON. | ||
* `method` - (Optional) HTTP request method. Default is `POST`. | ||
* `response` - Response message in JSON format. |