-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ksoclabs/Add_Azure_Support
Add Azure Support and Make AWS use Generic Registration Endpoint
- Loading branch information
Showing
8 changed files
with
192 additions
and
21 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,27 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "ksoc_azure_register Resource - terraform-provider-ksoc" | ||
subcategory: "" | ||
description: |- | ||
Register Azure Subscription and Tenant with Ksoc | ||
--- | ||
|
||
# ksoc_azure_register (Resource) | ||
|
||
Register Azure Subscription and Tenant with Ksoc | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `subscription_id` (String) Subscription ID to use | ||
- `tenant_id` (String, Sensitive) Azure Tenant to use when gathering resources | ||
|
||
### Read-Only | ||
|
||
- `api_path` (String) Target of the API path | ||
- `id` (String) The ID of this resource. | ||
- `ksoc_registered` (Boolean) Target of the API path |
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,4 @@ | ||
resource "ksoc_azure_register" "this" { | ||
subscription_id = "123" | ||
tenant_id = "456" | ||
} |
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
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,120 @@ | ||
package ksoc | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/ksoclabs/terraform-provider-ksoc/internal/request" | ||
) | ||
|
||
func resourceAzureRegister() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Register Azure Subscription and Tenant with Ksoc", | ||
|
||
CreateContext: resourceAzureRegisterCreate, | ||
ReadContext: resourceAzureRegisterRead, | ||
UpdateContext: resourceAzureRegisterUpdate, | ||
DeleteContext: resourceAzureRegisterDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"subscription_id": { | ||
Type: schema.TypeString, | ||
Description: "Subscription ID to use", | ||
//ForceNew: true, | ||
Required: true, | ||
}, | ||
"tenant_id": { | ||
Type: schema.TypeString, | ||
Description: "Azure Tenant to use when gathering resources", | ||
ForceNew: true, | ||
Required: true, | ||
Sensitive: true, | ||
}, | ||
"ksoc_registered": { | ||
Type: schema.TypeBool, | ||
Description: "Target of the API path", | ||
Computed: true, | ||
}, | ||
|
||
// Computed values | ||
"api_path": { | ||
Type: schema.TypeString, | ||
Description: "Target of the API path", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAzureRegisterCreate(ctx context.Context, d *schema.ResourceData, meta any) (diags diag.Diagnostics) { | ||
config := meta.(*Config) | ||
httpMethod := http.MethodPost | ||
setValueOnSuccess := config.KsocApiUrl | ||
diags = resourceAzureRegisterGeneric(ctx, httpMethod, d, setValueOnSuccess, meta) | ||
return diags | ||
} | ||
|
||
func resourceAzureRegisterRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
config := meta.(*Config) | ||
apiUrlBase := config.KsocApiUrl | ||
targetURI := apiUrlBase + "/cloud/register" | ||
err := d.Set("api_path", targetURI) | ||
if err != nil { | ||
return diag.Errorf("Error setting api_path: %s", err) | ||
} | ||
return nil | ||
} | ||
|
||
func resourceAzureRegisterUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
// Update has not yet been implemented | ||
return nil | ||
} | ||
|
||
func resourceAzureRegisterDelete(ctx context.Context, d *schema.ResourceData, meta any) (diags diag.Diagnostics) { | ||
httpMethod := http.MethodDelete | ||
setValueOnSuccess := "" | ||
diags = resourceAzureRegisterGeneric(ctx, httpMethod, d, setValueOnSuccess, meta) | ||
return diags | ||
} | ||
|
||
func resourceAzureRegisterGeneric(ctx context.Context, httpMethod string, d *schema.ResourceData, setValueOnSuccess string, meta any) (diags diag.Diagnostics) { | ||
config := meta.(*Config) | ||
apiUrlBase := config.KsocApiUrl | ||
|
||
targetURI := apiUrlBase + "/cloud/register" | ||
accessKey := config.AccessKeyId | ||
secretKey := config.SecretKey | ||
|
||
tenantID := d.Get("tenant_id").(string) | ||
subscriptionId := d.Get("subscription_id").(string) | ||
|
||
payload := &RegistrationPayload{ | ||
Type: "azure", | ||
Credentials: Credentials{ | ||
AzureSubscription: AzureSubscriptionCredential{ | ||
TenantID: tenantID, | ||
SubscriptionID: subscriptionId, | ||
}, | ||
}, | ||
} | ||
|
||
statusCode, _, diags := request.AuthenticatedRequest(ctx, apiUrlBase, httpMethod, targetURI, accessKey, secretKey, payload) | ||
if statusCode != http.StatusOK { | ||
return append(diags, diag.Errorf("Failed to register with KSOC, received HTTP status: %d", statusCode)...) | ||
} | ||
|
||
err := d.Set("api_path", targetURI) | ||
if err != nil { | ||
return diag.Errorf("Error setting api_path: %s", err) | ||
} | ||
|
||
if err := d.Set("ksoc_registered", statusCode == http.StatusOK); err != nil { | ||
return append(diags, diag.Errorf("Error setting ksoc_registered: %s", err)...) | ||
} | ||
|
||
d.SetId(setValueOnSuccess) | ||
|
||
return nil | ||
} |