pulumi/pkg/resource/resource_id.go
Luke Hoban ae03d69645 Wire up APIs to lambdas using output properties
We now have enough output properties implementation
working to change our API gateway examples and API
wrapper to correctly wire the API routes to the ARNs of
lambdas passed in to them.

We both wire up the lambda to the route, but also create
a permission specific to each route to assign to the
corresponding lambda - providing least privelege needed
for the API definition.

Also adds `string#toUpperCase` and fixes NewUniqueHex
to match how we are using it.
2017-06-15 16:01:00 -07:00

88 lines
2.7 KiB
Go

// Licensed to Pulumi Corporation ("Pulumi") under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// Pulumi licenses this file to You 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 resource
import (
"crypto/rand"
"encoding/hex"
"github.com/pulumi/lumi/pkg/eval/rt"
"github.com/pulumi/lumi/pkg/util/contract"
)
// ID is a unique resource identifier; it is managed by the provider and is mostly opaque to Lumi.
type ID string
// String converts a resource ID into a string.
func (id ID) String() string {
return string(id)
}
// StringPtr converts an optional ID into an optional string.
func (id *ID) StringPtr() *string {
if id == nil {
return nil
}
ids := (*id).String()
return &ids
}
// IDStrings turns an array of resource IDs into an array of strings.
func IDStrings(ids []ID) []string {
ss := make([]string, len(ids))
for i, id := range ids {
ss[i] = id.String()
}
return ss
}
// MaybeID turns an optional string into an optional resource ID.
func MaybeID(s *string) *ID {
var ret *ID
if s != nil {
id := ID(*s)
ret = &id
}
return ret
}
const (
// IDProperty is the special ID property name.
IDProperty = rt.PropertyKey("id")
// IDPropertyKey is the special ID property name for resource maps.
IDPropertyKey = PropertyKey("id")
)
// NewUniqueHex generates a new "random" hex string for use by resource providers. It has the given optional prefix and
// the total length is capped to the maxlen. Note that capping to maxlen necessarily increases the risk of collisions.
func NewUniqueHex(prefix string, maxlen, randlen int) string {
bs := make([]byte, randlen)
n, err := rand.Read(bs)
contract.Assert(err == nil)
contract.Assert(n == len(bs))
str := prefix + hex.EncodeToString(bs)
if len(str) > maxlen {
str = str[:maxlen]
}
return str
}
// NewUniqueHexID generates a new "random" hex ID for use by resource providers. It has the given optional prefix and
// the total length is capped to the maxlen. Note that capping to maxlen necessarily increases the risk of collisions.
func NewUniqueHexID(prefix string, maxlen, randlen int) ID {
return ID(NewUniqueHex(prefix, randlen, maxlen))
}