Skip to content

Commit

Permalink
Merge branch 'main' into feat/golden_state
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinhard-Pilz-Dynatrace committed Aug 8, 2024
2 parents 97b7e87 + 52f20bf commit 81e2476
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 2 deletions.
111 changes: 111 additions & 0 deletions datasources/apitoken/data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* @license
* Copyright 2020 Dynatrace LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package apitoken

import (
"context"

"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api"
"github.com/dynatrace-oss/terraform-provider-dynatrace/provider/config"
"github.com/dynatrace-oss/terraform-provider-dynatrace/provider/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

srv "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v2/apitokens"
apitokens "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v2/apitokens/settings"
)

func DataSource() *schema.Resource {
return &schema.Resource{
ReadContext: logging.EnableDSCtx(DataSourceRead),
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"enabled": {
Type: schema.TypeBool,
Description: "The token is enabled (true) or disabled (false), default disabled (false).",
Computed: true,
},
"personal_access_token": {
Type: schema.TypeBool,
Description: "The token is a personal access token (true) or an API token (false).",
Computed: true,
},
"expiration_date": {
Type: schema.TypeString,
Description: "The expiration date of the token.",
Computed: true,
},
"owner": {
Type: schema.TypeString,
Description: "The owner of the token",
Computed: true,
},
"creation_date": {
Type: schema.TypeString,
Description: "Token creation date in ISO 8601 format (yyyy-MM-dd'T'HH:mm:ss.SSS'Z')",
Computed: true,
},
"scopes": {
Type: schema.TypeSet,
Description: "A list of the scopes to be assigned to the token.",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func DataSourceRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
var name string
if v, ok := d.GetOk("name"); ok {
name = v.(string)
}
creds, err := config.Credentials(m, config.CredValDefault)
if err != nil {
return diag.FromErr(err)
}

service := srv.Service(creds)
var stubs api.Stubs
if stubs, err = service.List(ctx); err != nil {
return diag.FromErr(err)
}

if len(stubs) > 0 {
for _, stub := range stubs {
if name == stub.Name {
d.SetId(stub.ID)

value := stub.Value.(*apitokens.APIToken)
d.Set("enabled", value.Enabled)
d.Set("personal_access_token", value.PersonalAccessToken)
d.Set("expiration_date", value.ExpirationDate)
d.Set("owner", value.Owner)
d.Set("creation_date", value.CreationDate)
d.Set("scopes", value.Scopes)

return diag.Diagnostics{}
}
}
}
d.SetId("")
return diag.Diagnostics{}
}
77 changes: 77 additions & 0 deletions datasources/apitoken/data_source_multiple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @license
* Copyright 2020 Dynatrace LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package apitoken

import (
"context"

"github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api"
"github.com/dynatrace-oss/terraform-provider-dynatrace/provider/config"
"github.com/dynatrace-oss/terraform-provider-dynatrace/provider/logging"
"github.com/dynatrace-oss/terraform-provider-dynatrace/terraform/hcl"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

srv "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v2/apitokens"
apitokens "github.com/dynatrace-oss/terraform-provider-dynatrace/dynatrace/api/v2/apitokens/settings"
)

func DataSourceMultiple() *schema.Resource {
return &schema.Resource{
ReadContext: logging.EnableDSCtx(DataSourceReadMultiple),
Schema: map[string]*schema.Schema{
"api_tokens": {
Type: schema.TypeList,
Elem: &schema.Resource{Schema: new(apitokens.APIToken).Schema()},
Computed: true,
},
},
}
}

func DataSourceReadMultiple(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
d.SetId("dynatrace_api_tokens")

creds, err := config.Credentials(m, config.CredValDefault)
if err != nil {
return diag.FromErr(err)
}

service := srv.Service(creds)
var stubs api.Stubs
if stubs, err = service.List(ctx); err != nil {
return diag.FromErr(err)
}

if len(stubs) > 0 {
tokens := []*apitokens.APIToken{}
for _, stub := range stubs {
value := stub.Value.(*apitokens.APIToken)
tokens = append(tokens, value)
}

marshalled := hcl.Properties{}
if marshalled.EncodeSlice("api_tokens", tokens); err != nil {
return diag.FromErr(err)
}

d.Set("api_tokens", marshalled["api_tokens"])
}

return diag.Diagnostics{}
}
4 changes: 2 additions & 2 deletions dynatrace/api/v2/apitokens/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ func (me *service) List(ctx context.Context) (api.Stubs, error) {
var err error

client := rest.DefaultClient(me.credentials.URL, me.credentials.Token)
req := client.Get("/api/v2/apiTokens?pageSize=10000&sort=-creationDate").Expect(200)
req := client.Get("/api/v2/apiTokens?pageSize=10000&fields=%2Bscopes%2C%2BexpirationDate%2C%2BpersonalAccessToken&sort=-creationDate").Expect(200)
var tokenlist apitokens.TokenList
if err = req.Finish(&tokenlist); err != nil {
return nil, err
}
stubs := api.Stubs{}
for _, token := range tokenlist.APITokens {
stubs = append(stubs, &api.Stub{ID: *token.ID, Name: token.Name})
stubs = append(stubs, &api.Stub{ID: *token.ID, Name: token.Name, Value: &token.APIToken})
}

return stubs, nil
Expand Down
3 changes: 3 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"

"github.com/dynatrace-oss/terraform-provider-dynatrace/datasources/alerting"
"github.com/dynatrace-oss/terraform-provider-dynatrace/datasources/apitoken"
"github.com/dynatrace-oss/terraform-provider-dynatrace/datasources/application"
"github.com/dynatrace-oss/terraform-provider-dynatrace/datasources/appsec/attackalerting"
"github.com/dynatrace-oss/terraform-provider-dynatrace/datasources/appsec/vulnerabilityalerting"
Expand Down Expand Up @@ -228,6 +229,8 @@ func Provider() *schema.Provider {
"dynatrace_autotag": autotag.DataSource(),
"dynatrace_generic_settings": genericsettingsds.DataSourceMultiple(),
"dynatrace_generic_setting": genericsettingsds.DataSource(),
"dynatrace_api_tokens": apitoken.DataSourceMultiple(),
"dynatrace_api_token": apitoken.DataSource(),
},
ResourcesMap: map[string]*schema.Resource{
"dynatrace_custom_service": resources.NewGeneric(export.ResourceTypes.CustomService).Resource(),
Expand Down
27 changes: 27 additions & 0 deletions templates/data-sources/api_token.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
layout: ""
page_title: "dynatrace_api_token Data Source - terraform-provider-dynatrace"
subcategory: "Access Tokens"
description: |-
The data source `dynatrace_api_token` covers queries for an access token
---

# dynatrace_api_token (Data Source)

The API token data source allows a single access token to be retrieved by its name, note the token value is not included in the response.

If multiple tokens match the given name, the first result will be retrieved. To retrieve multiple tokens of the same name, please utilize the `dynatrace_api_tokens` data source.

## Example Usage

```terraform
data "dynatrace_api_token" "example" {
name = "Terraform"
}

output "example" {
value = data.dynatrace_api_token.example
}
```

{{ .SchemaMarkdown | trimspace }}
24 changes: 24 additions & 0 deletions templates/data-sources/api_tokens.md.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
layout: ""
page_title: "dynatrace_api_tokens Data Source - terraform-provider-dynatrace"
subcategory: "Access Tokens"
description: |-
The data source `dynatrace_api_tokens` covers queries for a list of access tokens
---

# dynatrace_api_tokens (Data Source)

The API tokens data source allows all access tokens to be retrieved, note the token value is not included in the response.

## Example Usage

```terraform
data "dynatrace_api_tokens" "example" {
}

output "example" {
value = data.dynatrace_api_tokens.example
}
```

{{ .SchemaMarkdown | trimspace }}

0 comments on commit 81e2476

Please sign in to comment.