Skip to content

Commit

Permalink
implement grpcwrap identity schema methods
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Feb 14, 2025
1 parent d70c9ff commit f204824
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 13 deletions.
22 changes: 17 additions & 5 deletions internal/grpcwrap/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ import (
// implementation.
func Provider(p providers.Interface) tfplugin5.ProviderServer {
return &provider{
provider: p,
schema: p.GetProviderSchema(),
provider: p,
schema: p.GetProviderSchema(),
identitySchemas: p.GetResourceIdentitySchemas(),
}
}

type provider struct {
provider providers.Interface
schema providers.GetProviderSchemaResponse
provider providers.Interface
schema providers.GetProviderSchemaResponse
identitySchemas providers.GetResourceIdentitySchemasResponse
}

func (p *provider) GetMetadata(_ context.Context, req *tfplugin5.GetMetadata_Request) (*tfplugin5.GetMetadata_Response, error) {
Expand Down Expand Up @@ -540,7 +542,17 @@ func (p *provider) CallFunction(_ context.Context, req *tfplugin5.CallFunction_R
}

func (p *provider) GetResourceIdentitySchemas(_ context.Context, req *tfplugin5.GetResourceIdentitySchemas_Request) (*tfplugin5.GetResourceIdentitySchemas_Response, error) {
panic("Not implemented yet")
resp := &tfplugin5.GetResourceIdentitySchemas_Response{
IdentitySchemas: map[string]*tfplugin5.ResourceIdentitySchema{},
Diagnostics: []*tfplugin5.Diagnostic{},
}

for name, schema := range p.identitySchemas.IdentityTypes {
resp.IdentitySchemas[name] = convert.ResourceIdentitySchemaToProto(schema)
}

resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, p.identitySchemas.Diagnostics)
return resp, nil
}

func (p *provider) UpgradeResourceIdentity(_ context.Context, req *tfplugin5.UpgradeResourceIdentity_Request) (*tfplugin5.UpgradeResourceIdentity_Response, error) {
Expand Down
22 changes: 17 additions & 5 deletions internal/grpcwrap/provider6.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ import (
// internal provider implementation.
func Provider6(p providers.Interface) tfplugin6.ProviderServer {
return &provider6{
provider: p,
schema: p.GetProviderSchema(),
provider: p,
schema: p.GetProviderSchema(),
identitySchemas: p.GetResourceIdentitySchemas(),
}
}

type provider6 struct {
provider providers.Interface
schema providers.GetProviderSchemaResponse
provider providers.Interface
schema providers.GetProviderSchemaResponse
identitySchemas providers.GetResourceIdentitySchemasResponse
}

func (p *provider6) GetMetadata(_ context.Context, req *tfplugin6.GetMetadata_Request) (*tfplugin6.GetMetadata_Response, error) {
Expand Down Expand Up @@ -590,7 +592,17 @@ func (p *provider6) CallFunction(_ context.Context, req *tfplugin6.CallFunction_
}

func (p *provider6) GetResourceIdentitySchemas(_ context.Context, req *tfplugin6.GetResourceIdentitySchemas_Request) (*tfplugin6.GetResourceIdentitySchemas_Response, error) {
panic("Not implemented yet")
resp := &tfplugin6.GetResourceIdentitySchemas_Response{
IdentitySchemas: map[string]*tfplugin6.ResourceIdentitySchema{},
Diagnostics: []*tfplugin6.Diagnostic{},
}

for name, schema := range p.identitySchemas.IdentityTypes {
resp.IdentitySchemas[name] = convert.ResourceIdentitySchemaToProto(schema)
}

resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, p.identitySchemas.Diagnostics)
return resp, nil
}

func (p *provider6) UpgradeResourceIdentity(_ context.Context, req *tfplugin6.UpgradeResourceIdentity_Request) (*tfplugin6.UpgradeResourceIdentity_Response, error) {
Expand Down
28 changes: 28 additions & 0 deletions internal/plugin/convert/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,31 @@ func ProtoToResourceIdentitySchema(s *proto.ResourceIdentitySchema) (providers.I

return schema, diags
}

func ResourceIdentitySchemaToProto(b providers.IdentitySchema) *proto.ResourceIdentitySchema {
attrs := []*proto.ResourceIdentitySchema_IdentityAttribute{}
for _, name := range sortedKeys(b.Attributes) {
a := b.Attributes[name]

attr := &proto.ResourceIdentitySchema_IdentityAttribute{
Name: name,
Description: a.Description,
RequiredForImport: a.RequiredForImport,
OptionalForImport: a.OptionalForImport,
}

ty, err := json.Marshal(a.Type)
if err != nil {
panic(err)
}

attr.Type = ty

attrs = append(attrs, attr)
}

return &proto.ResourceIdentitySchema{
Version: b.Version,
IdentityAttributes: attrs,
}
}
29 changes: 29 additions & 0 deletions internal/plugin6/convert/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,32 @@ func configschemaObjectToProto(b *configschema.Object) *proto.Schema_Object {
Nesting: nesting,
}
}

func ResourceIdentitySchemaToProto(schema providers.IdentitySchema) *proto.ResourceIdentitySchema {
identityAttributes := []*proto.ResourceIdentitySchema_IdentityAttribute{}

for _, name := range sortedKeys(schema.Attributes) {
a := schema.Attributes[name]
attr := &proto.ResourceIdentitySchema_IdentityAttribute{
Name: name,
Description: a.Description,
RequiredForImport: a.RequiredForImport,
OptionalForImport: a.OptionalForImport,
}

if a.Type != cty.NilType {
ty, err := json.Marshal(a.Type)
if err != nil {
panic(err)
}
attr.Type = ty
}

identityAttributes = append(identityAttributes, attr)
}

return &proto.ResourceIdentitySchema{
Version: schema.Version,
IdentityAttributes: identityAttributes,
}
}
1 change: 1 addition & 0 deletions internal/provider-simple/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (s simple) GetResourceIdentitySchemas() providers.GetResourceIdentitySchema
"id": {
Type: cty.String,
RequiredForImport: true,
OptionalForImport: false,
},
},
},
Expand Down
12 changes: 9 additions & 3 deletions internal/providers/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ type Mock struct {
Provider Interface
Data *configs.MockData

schema *GetProviderSchemaResponse
schema *GetProviderSchemaResponse
identitySchema *GetResourceIdentitySchemasResponse
}

func (m *Mock) GetProviderSchema() GetProviderSchemaResponse {
Expand Down Expand Up @@ -67,8 +68,13 @@ func (m *Mock) GetProviderSchema() GetProviderSchemaResponse {
}

func (m *Mock) GetResourceIdentitySchemas() GetResourceIdentitySchemasResponse {
// TODO
return GetResourceIdentitySchemasResponse{}
if m.identitySchema == nil {
// Cache the schema, it's not changing.
schema := m.Provider.GetResourceIdentitySchemas()

m.identitySchema = &schema
}
return *m.identitySchema
}

func (m *Mock) ValidateProviderConfig(request ValidateProviderConfigRequest) (response ValidateProviderConfigResponse) {
Expand Down

0 comments on commit f204824

Please sign in to comment.