pulumi/cmd/pulumi-test-language/providers/bad_provider.go

126 lines
3.5 KiB
Go

// Copyright 2016-2023, Pulumi Corporation.
//
// 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 providers
import (
"context"
"encoding/json"
"fmt"
"github.com/blang/semver"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)
type BadProvider struct {
plugin.UnimplementedProvider
}
var _ plugin.Provider = (*BadProvider)(nil)
func (p *BadProvider) Close() error {
return nil
}
func (p *BadProvider) Pkg() tokens.Package {
return "bad"
}
func (p *BadProvider) GetSchema(_ context.Context, request plugin.GetSchemaRequest) (plugin.GetSchemaResponse, error) {
// The whole point of this provider is to return an invalid schema, so just make up a type for the
// property value.
resourceProperties := map[string]schema.PropertySpec{
"value": {
TypeSpec: schema.TypeSpec{
Type: "not a type",
},
},
}
resourceRequired := []string{"value"}
pkg := schema.PackageSpec{
Name: "bad",
Version: "3.0.0",
Resources: map[string]schema.ResourceSpec{
"bad:index:Resource": {
ObjectTypeSpec: schema.ObjectTypeSpec{
Type: "object",
Properties: resourceProperties,
Required: resourceRequired,
},
InputProperties: resourceProperties,
RequiredInputs: resourceRequired,
},
},
}
jsonBytes, err := json.Marshal(pkg)
if err != nil {
return plugin.GetSchemaResponse{}, err
}
return plugin.GetSchemaResponse{Schema: jsonBytes}, nil
}
func (p *BadProvider) CheckConfig(
_ context.Context, req plugin.CheckConfigRequest,
) (plugin.CheckConfigResponse, error) {
// Expect just the version
version, ok := req.News["version"]
if !ok {
return plugin.CheckConfigResponse{Failures: makeCheckFailure("version", "missing version")}, nil
}
if !version.IsString() {
return plugin.CheckConfigResponse{Failures: makeCheckFailure("version", "version is not a string")}, nil
}
if version.StringValue() != "3.0.0" {
return plugin.CheckConfigResponse{Failures: makeCheckFailure("version", "version is not 3.0.0")}, nil
}
if len(req.News) != 1 {
return plugin.CheckConfigResponse{
Failures: makeCheckFailure("", fmt.Sprintf("too many properties: %v", req.News)),
}, nil
}
return plugin.CheckConfigResponse{Properties: req.News}, nil
}
func (p *BadProvider) Configure(context.Context, plugin.ConfigureRequest) (plugin.ConfigureResponse, error) {
return plugin.ConfigureResponse{}, nil
}
func (p *BadProvider) GetPluginInfo(context.Context) (workspace.PluginInfo, error) {
ver := semver.MustParse("3.0.0")
return workspace.PluginInfo{
Version: &ver,
}, nil
}
func (p *BadProvider) SignalCancellation(context.Context) error {
return nil
}
func (p *BadProvider) GetMapping(context.Context, plugin.GetMappingRequest) (plugin.GetMappingResponse, error) {
return plugin.GetMappingResponse{}, nil
}
func (p *BadProvider) GetMappings(context.Context, plugin.GetMappingsRequest) (plugin.GetMappingsResponse, error) {
return plugin.GetMappingsResponse{}, nil
}