Skip to content
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

feat: add write_token field in resource/datasource ovh_dbaas_logs_output_graylog_stream #689

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions ovh/data_dbaas_logs_output_graylog_stream.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package ovh

import (
"context"
"fmt"
"log"
"net/url"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceDbaasLogsOutputGraylogStream() *schema.Resource {
return &schema.Resource{
Read: func(d *schema.ResourceData, meta interface{}) error {
return dataSourceDbaasLogsOutputGraylogStreamRead(d, meta)
},
ReadContext: dataSourceDbaasLogsOutputGraylogStreamRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -137,11 +137,17 @@ func dataSourceDbaasLogsOutputGraylogStream() *schema.Resource {
Description: "Enable Websocket",
Computed: true,
},
"write_token": {
Type: schema.TypeString,
Description: "Write token of the stream",
Computed: true,
Sensitive: true,
},
},
}
}

func dataSourceDbaasLogsOutputGraylogStreamRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceDbaasLogsOutputGraylogStreamRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)

serviceName := d.Get("service_name").(string)
Expand All @@ -154,7 +160,7 @@ func dataSourceDbaasLogsOutputGraylogStreamRead(d *schema.ResourceData, meta int
url.PathEscape(serviceName),
)
if err := config.OVHClient.Get(endpoint, &res); err != nil {
return fmt.Errorf("Error calling Get %s:\n\t %q", endpoint, err)
return diag.Errorf("Error calling Get %s:\n\t %q", endpoint, err)
}
streams := []*DbaasLogsOutputGraylogStream{}

Expand All @@ -169,7 +175,7 @@ func dataSourceDbaasLogsOutputGraylogStreamRead(d *schema.ResourceData, meta int

stream := &DbaasLogsOutputGraylogStream{}
if err := config.OVHClient.Get(endpoint, &stream); err != nil {
return fmt.Errorf("Error calling Get %s:\n\t %q", endpoint, err)
return diag.Errorf("Error calling Get %s:\n\t %q", endpoint, err)
}

log.Printf("[INFO]Comparing : %s ? %s",
Expand All @@ -183,12 +189,10 @@ func dataSourceDbaasLogsOutputGraylogStreamRead(d *schema.ResourceData, meta int
}

if len(streams) == 0 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
return diag.Errorf("Your query returned no results. Please change your search criteria and try again.")
}
if len(streams) > 1 {
return fmt.Errorf("Your query returned more than one result. " +
"Please change your search criteria and try again.")
return diag.Errorf("Your query returned more than one result. Please change your search criteria and try again.")
}

for k, v := range streams[0].ToMap() {
Expand All @@ -200,5 +204,12 @@ func dataSourceDbaasLogsOutputGraylogStreamRead(d *schema.ResourceData, meta int
}
}

// Get stream write token, if available
writeToken, err := resourceDbaasLogsOutputGraylogStreamGetWriteToken(ctx, config, serviceName, streams[0].StreamId)
if err != nil {
return diag.FromErr(err)
}
d.Set("write_token", writeToken)

return nil
}
4 changes: 4 additions & 0 deletions ovh/data_dbaas_logs_output_graylog_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func TestAccDataSourceDbaasLogsOutputGraylogStream_basic(t *testing.T) {
"title",
title,
),
resource.TestCheckResourceAttrSet(
"data.ovh_dbaas_logs_output_graylog_stream.stream",
"write_token",
),
),
},
},
Expand Down
39 changes: 39 additions & 0 deletions ovh/resource_dbaas_logs_output_graylog_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ func resourceDbaasLogsOutputGraylogStream() *schema.Resource {
Description: "Stream last update",
Computed: true,
},
"write_token": {
Type: schema.TypeString,
Description: "Write token of the stream",
Computed: true,
Sensitive: true,
},
},
}
}
Expand Down Expand Up @@ -302,9 +308,42 @@ func resourceDbaasLogsOutputGraylogStreamRead(ctx context.Context, d *schema.Res
}
}

// Get stream write token, if available
writeToken, err := resourceDbaasLogsOutputGraylogStreamGetWriteToken(ctx, config, serviceName, id)
if err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}
d.Set("write_token", writeToken)

return nil
}

func resourceDbaasLogsOutputGraylogStreamGetWriteToken(ctx context.Context, config *Config, serviceName, streamId string) (string, error) {
var (
ruleIds []string
endpoint = fmt.Sprintf("/dbaas/logs/%s/output/graylog/stream/%s/rule", url.PathEscape(serviceName), url.PathEscape(streamId))
)

if err := config.OVHClient.GetWithContext(ctx, endpoint, &ruleIds); err != nil {
return "", fmt.Errorf("failed to list stream rules: %w", err)
}

for _, ruleId := range ruleIds {
rule := DbaasLogsOutputGraylogStreamRule{}
ruleEndpoint := endpoint + "/" + url.PathEscape(ruleId)

if err := config.OVHClient.GetWithContext(ctx, ruleEndpoint, &rule); err != nil {
return "", fmt.Errorf("failed to get stream rule %q: %w", ruleId, err)
}

if rule.Field == "X-OVH-TOKEN" {
return rule.Value, nil
}
}

return "", nil
}

func resourceDbaasLogsOutputGraylogStreamDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)

Expand Down
4 changes: 4 additions & 0 deletions ovh/resource_dbaas_logs_output_graylog_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func TestAccResourceDbaasLogsOutputGraylogStream_basic(t *testing.T) {
"title",
title,
),
resource.TestCheckResourceAttrSet(
"ovh_dbaas_logs_output_graylog_stream.stream",
"write_token",
),
),
},
},
Expand Down
5 changes: 5 additions & 0 deletions ovh/types_dbaas_logs_output_graylog_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,8 @@ func (opts *DbaasLogsOutputGraylogStreamUpdateOpts) FromResource(d *schema.Resou

return opts
}

type DbaasLogsOutputGraylogStreamRule struct {
Field string `json:"field"`
Value string `json:"value"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ data "ovh_dbaas_logs_output_graylog_stream" "stream" {
* `stream_id` - Stream ID
* `updated_at` - Stream last update
* `web_socket_enabled` - Enable Websocket
* `write_token` - Write token of the stream (empty if the caller is not the owner of the stream)
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ Id is set to the output stream Id. In addition, the following attributes are exp
* `nb_archive` - Number of coldstored archivesr
* `stream_id` - Stream ID
* `updated_at` - Stream last updater
* `write_token` - Write token of the stream (empty if the caller is not the owner of the stream)