Skip to content

Commit

Permalink
provider: add a login data source for getting the current user (#68)
Browse files Browse the repository at this point in the history
fixes #46

Signed-off-by: Nick Santos <[email protected]>
  • Loading branch information
nicks authored Oct 24, 2024
1 parent 4b9ff42 commit d6e621a
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/hubclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,7 @@ func (c *Client) sendRequest(ctx context.Context, method string, url string, bod

return nil
}

func (c *Client) Username() string {
return c.auth.Username
}
94 changes: 94 additions & 0 deletions internal/provider/data_source_login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2024 Docker Terraform Provider authors
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 provider

import (
"context"
"fmt"

"github.com/docker/terraform-provider-docker/internal/hubclient"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
_ datasource.DataSource = &LoginDataSource{}
_ datasource.DataSourceWithConfigure = &LoginDataSource{}
)

func NewLoginDataSource() datasource.DataSource {
return &LoginDataSource{}
}

type LoginDataSource struct {
client *hubclient.Client
}

type LoginDataSourceModel struct {
Username types.String `tfsdk:"username"`
}

func (d *LoginDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_login"
}

func (d *LoginDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: `Reads current login info.`,

Attributes: map[string]schema.Attribute{
"username": schema.StringAttribute{
MarkdownDescription: "Current username",
Computed: true,
},
},
}
}

func (d *LoginDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*hubclient.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *hubclient.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.client = client
}

func (d *LoginDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data LoginDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

data.Username = types.StringValue(d.client.Username())

diags := resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
38 changes: 38 additions & 0 deletions internal/provider/data_source_login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2024 Docker Terraform Provider authors
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 provider

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestLoginDataSource(t *testing.T) {
expected := os.Getenv("DOCKER_USERNAME")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: `data "docker_login" "_" {}`,
Check: resource.TestCheckResourceAttr("data.docker_login._", "username", expected),
},
},
})
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func (p *DockerProvider) DataSources(ctx context.Context) []func() datasource.Da
NewAccessTokenDataSource,
NewAccessTokensDataSource,
NewOrgTeamDataSource,
NewLoginDataSource,
}
}

Expand Down

0 comments on commit d6e621a

Please sign in to comment.