-
Notifications
You must be signed in to change notification settings - Fork 286
feat: create federated log provider #3044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
voorepreethi
wants to merge
6
commits into
newrelic:main
Choose a base branch
from
voorepreethi:fedLogsSetup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+851
−8
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
79d6958
feat: create federated log provider
voorepreethi 88fea11
feat: add aws connection provider
voorepreethi 2926009
feat: add aws connection provider
voorepreethi 80ba2ca
fix: fed log scope fix
voorepreethi 2ebd15c
feat: partition provider changes
voorepreethi 8c91b8d
fix: test case fix
voorepreethi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,263 @@ | ||
| package newrelic | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
| "strconv" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| "github.com/newrelic/newrelic-client-go/v2/pkg/pipelinecontrol" | ||
| ) | ||
|
|
||
| func resourceNewRelicAwsConnection() *schema.Resource { | ||
| return &schema.Resource{ | ||
| CreateContext: resourceNewRelicAwsConnectionCreate, | ||
| ReadContext: resourceNewRelicAwsConnectionRead, | ||
| UpdateContext: resourceNewRelicAwsConnectionUpdate, | ||
| DeleteContext: resourceNewRelicAwsConnectionDelete, | ||
| Importer: &schema.ResourceImporter{ | ||
| StateContext: schema.ImportStatePassthroughContext, | ||
| }, | ||
| Schema: map[string]*schema.Schema{ | ||
| "account_id": { | ||
| Type: schema.TypeInt, | ||
| Optional: true, | ||
| ForceNew: true, | ||
| Computed: true, | ||
| Description: "The account ID where the AWS connection will be created. Used when scope_type is ACCOUNT.", | ||
| }, | ||
| "scope_id": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| ForceNew: true, | ||
| Computed: true, | ||
| Description: "The scope ID (account ID or organization ID) for the AWS connection.", | ||
| }, | ||
| "scope_type": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| ForceNew: true, | ||
| Default: "ACCOUNT", | ||
| Description: "The scope type for the AWS connection. Valid values are ACCOUNT and ORGANIZATION.", | ||
| }, | ||
| "name": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The name of the AWS connection.", | ||
| }, | ||
| "description": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| Description: "The description of the AWS connection.", | ||
| }, | ||
| "enabled": { | ||
| Type: schema.TypeBool, | ||
| Optional: true, | ||
| Default: true, | ||
| Description: "Flag to indicate if the connection is enabled. True by default.", | ||
| }, | ||
| "external_id": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| Description: "Optional field representing an identifier managed by the consumer.", | ||
| }, | ||
| "region": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| Description: "Default region for this connection.", | ||
| }, | ||
| "role_arn": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The ARN of the IAM role to assume for this connection.", | ||
| }, | ||
| "settings": { | ||
| Type: schema.TypeList, | ||
| Optional: true, | ||
| Description: "Optional list of connection settings.", | ||
| Elem: &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "key": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The key or name of the setting.", | ||
| }, | ||
| "value": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The value of the setting.", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func resourceNewRelicAwsConnectionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| providerConfig := meta.(*ProviderConfig) | ||
| client := providerConfig.NewClient | ||
|
|
||
| // Determine scope | ||
| scopeType := d.Get("scope_type").(string) | ||
| var scopeID string | ||
|
|
||
| if v, ok := d.GetOk("scope_id"); ok { | ||
| scopeID = v.(string) | ||
| } else { | ||
| accountID := selectAccountID(providerConfig, d) | ||
| scopeID = strconv.Itoa(accountID) | ||
| _ = d.Set("account_id", accountID) | ||
| } | ||
|
|
||
| input := pipelinecontrol.EntityManagementAwsConnectionEntityCreateInput{ | ||
| Name: d.Get("name").(string), | ||
| Enabled: d.Get("enabled").(bool), | ||
| Credential: pipelinecontrol.EntityManagementAwsCredentialsCreateInput{ | ||
| AssumeRole: pipelinecontrol.EntityManagementAwsAssumeRoleConfigCreateInput{ | ||
| RoleArn: d.Get("role_arn").(string), | ||
| }, | ||
| }, | ||
| Scope: pipelinecontrol.EntityManagementScopedReferenceInput{ | ||
| Type: pipelinecontrol.EntityManagementEntityScope(scopeType), | ||
| ID: scopeID, | ||
| }, | ||
| } | ||
|
|
||
| if v, ok := d.GetOk("description"); ok { | ||
| input.Description = v.(string) | ||
| } | ||
| if v, ok := d.GetOk("external_id"); ok { | ||
| input.ExternalId = v.(string) | ||
| } | ||
| if v, ok := d.GetOk("region"); ok { | ||
| input.Region = v.(string) | ||
| } | ||
| if v, ok := d.GetOk("settings"); ok { | ||
| input.Settings = expandAwsConnectionSettings(v.([]interface{})) | ||
| } | ||
|
|
||
| resp, err := client.Pipelinecontrol.EntityManagementCreateAwsConnectionWithContext(ctx, input) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| d.SetId(resp.Entity.ID) | ||
|
|
||
| return resourceNewRelicAwsConnectionRead(ctx, d, meta) | ||
| } | ||
|
|
||
| func resourceNewRelicAwsConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| providerConfig := meta.(*ProviderConfig) | ||
| client := providerConfig.NewClient | ||
|
|
||
| log.Printf("[INFO] Reading New Relic AWS Connection for %s", d.Id()) | ||
|
|
||
| entityID := d.Id() | ||
|
|
||
| resp, err := client.Pipelinecontrol.GetEntityWithContext(ctx, entityID) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| if resp == nil { | ||
| d.SetId("") | ||
| return diag.FromErr(fmt.Errorf("entity with ID %s was nil", entityID)) | ||
| } | ||
|
|
||
| switch entityType := (*resp).(type) { | ||
| case *pipelinecontrol.EntityManagementAwsConnectionEntity: | ||
| entity := (*resp).(*pipelinecontrol.EntityManagementAwsConnectionEntity) | ||
|
|
||
| accountIDStr := entity.Scope.ID | ||
| accountIDInt, accountIDIntErr := strconv.Atoi(accountIDStr) | ||
| if accountIDIntErr != nil { | ||
| log.Printf("[ERROR] Failed to convert account ID to integer: %v", accountIDIntErr) | ||
| accountIDInt = selectAccountID(providerConfig, d) | ||
| log.Printf("[INFO] Assigning the value of account_id from the state to prevent a panic: %d", accountIDInt) | ||
| } | ||
|
|
||
| if err := d.Set("account_id", accountIDInt); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("scope_id", entity.Scope.ID); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("scope_type", string(entity.Scope.Type)); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("name", entity.Name); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("description", entity.Description); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("enabled", entity.Enabled); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("external_id", entity.ExternalId); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("region", entity.Region); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("role_arn", entity.Credential.AssumeRole.RoleArn); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("settings", flattenAwsConnectionSettings(entity.Settings)); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| default: | ||
| return diag.Errorf("unexpected entity type %T for ID %s", entityType, d.Id()) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func resourceNewRelicAwsConnectionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| // Update not yet supported for AwsConnectionEntity in the client | ||
| return resourceNewRelicAwsConnectionRead(ctx, d, meta) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't we add it? |
||
| } | ||
|
|
||
| func resourceNewRelicAwsConnectionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| providerConfig := meta.(*ProviderConfig) | ||
| client := providerConfig.NewClient | ||
|
|
||
| log.Printf("[INFO] Deleting New Relic AWS Connection entity with ID %s", d.Id()) | ||
|
|
||
| entityID := d.Id() | ||
|
|
||
| result, err := client.Pipelinecontrol.EntityManagementDelete(entityID) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| if result.ID == "" { | ||
| return diag.FromErr(fmt.Errorf("error in deleting entity with ID %s", entityID)) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func expandAwsConnectionSettings(settings []interface{}) []pipelinecontrol.EntityManagementConnectionSettingsCreateInput { | ||
| result := make([]pipelinecontrol.EntityManagementConnectionSettingsCreateInput, len(settings)) | ||
| for i, s := range settings { | ||
| setting := s.(map[string]interface{}) | ||
| result[i] = pipelinecontrol.EntityManagementConnectionSettingsCreateInput{ | ||
| Key: setting["key"].(string), | ||
| Value: setting["value"].(string), | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func flattenAwsConnectionSettings(settings []pipelinecontrol.EntityManagementConnectionSettings) []map[string]interface{} { | ||
| result := make([]map[string]interface{}, len(settings)) | ||
| for i, s := range settings { | ||
| result[i] = map[string]interface{}{ | ||
| "key": s.Key, | ||
| "value": s.Value, | ||
| } | ||
| } | ||
| return result | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we use
federated_logshere and elsewhere instead, this will becomenewrelic_federated_logs_setup