|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "sync" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +// Product defines the shared configuration for a single Magic Modules product. It is |
| 11 | +// defined in `product.yaml`. |
| 12 | +type Product struct { |
| 13 | + // Name is the product name in lower case, e.g,. "alloydb". |
| 14 | + Name string |
| 15 | + // BaseUrl is the base URL for API requests. It may contain Magic Modules templating directives. |
| 16 | + BaseUrl string |
| 17 | +} |
| 18 | + |
| 19 | +// Register adds the product definition to the internal product registry. |
| 20 | +func (p Product) Register() { |
| 21 | + products.Lock() |
| 22 | + defer products.Unlock() |
| 23 | + if _, ok := products.m[p.Name]; ok { |
| 24 | + log.Fatalf("Duplicate registration attempt for product %q", p.Name) |
| 25 | + } |
| 26 | + products.m[p.Name] = p |
| 27 | +} |
| 28 | + |
| 29 | +type registeredProducts struct { |
| 30 | + sync.RWMutex |
| 31 | + m map[string]Product |
| 32 | +} |
| 33 | + |
| 34 | +var products = ®isteredProducts{ |
| 35 | + m: make(map[string]Product), |
| 36 | +} |
| 37 | + |
| 38 | +// SchemaType differentitates a registered Terraform schema in cases where multiple schemas |
| 39 | +// share a name. For instance, resources and their corresponding data sources are identically named. |
| 40 | +type SchemaType int |
| 41 | + |
| 42 | +const ( |
| 43 | + SchemaTypeResource SchemaType = iota |
| 44 | + SchemaTypeIAMResource |
| 45 | + SchemaTypeDataSource |
| 46 | + SchemaTypeIAMDataSource |
| 47 | +) |
| 48 | + |
| 49 | +// IsDataSource is a helper method that returns whether a SchemaType refers to a data source or resource. |
| 50 | +func (s SchemaType) IsDataSource() bool { |
| 51 | + return s == SchemaTypeDataSource || s == SchemaTypeIAMDataSource |
| 52 | +} |
| 53 | + |
| 54 | +// Schema is used to configure a resource or data source within the registry. |
| 55 | +type Schema struct { |
| 56 | + // Name is the externally visible name, e.g., "google_alloydb_cluster". |
| 57 | + Name string |
| 58 | + // ProductName is the `Product` that this `Schema` is associated with. |
| 59 | + ProductName string |
| 60 | + // Type defines how the `Schema` should be registered. |
| 61 | + Type SchemaType |
| 62 | + // Schema contains the underlying Terraform schema. The data within is shared and assumed |
| 63 | + // to be immutable. |
| 64 | + Schema *schema.Resource |
| 65 | +} |
| 66 | + |
| 67 | +// Register adds the schema definition to the internal registry. |
| 68 | +func (s Schema) Register() { |
| 69 | + products.Lock() |
| 70 | + defer products.Unlock() |
| 71 | + if s.Type.IsDataSource() { |
| 72 | + if _, ok := schemas.d[s.Name]; ok { |
| 73 | + log.Fatalf("Duplicate registration attempt for data source %q", s.Name) |
| 74 | + } |
| 75 | + schemas.d[s.Name] = s |
| 76 | + } else { |
| 77 | + if _, ok := schemas.r[s.Name]; ok { |
| 78 | + log.Fatalf("Duplicate registration attempt for resource %q", s.Name) |
| 79 | + } |
| 80 | + schemas.r[s.Name] = s |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +type registeredSchemas struct { |
| 85 | + sync.RWMutex |
| 86 | + r map[string]Schema |
| 87 | + d map[string]Schema |
| 88 | +} |
| 89 | + |
| 90 | +var schemas = ®isteredSchemas{ |
| 91 | + r: make(map[string]Schema), |
| 92 | + d: make(map[string]Schema), |
| 93 | +} |
| 94 | + |
| 95 | +// Resource returns the Terraform schema for the requested resource. The function panics |
| 96 | +// if the requested resource is not registered. This function is called during provider |
| 97 | +// intitialization when the absence of a resource is an unrecoverable error. |
| 98 | +func Resource(name string) *schema.Resource { |
| 99 | + schemas.RLock() |
| 100 | + defer schemas.RUnlock() |
| 101 | + r, ok := schemas.r[name] |
| 102 | + if !ok { |
| 103 | + log.Fatalf("No resource schema for %q registered", name) |
| 104 | + } |
| 105 | + return r.Schema |
| 106 | +} |
| 107 | + |
| 108 | +// DataSource returns the Terraform schema for the requested data source. The function panics |
| 109 | +// if the requested data source is not registered. This function is called during provider |
| 110 | +// intitialization when the absence of a data source is an unrecoverable error. |
| 111 | +func DataSource(name string) *schema.Resource { |
| 112 | + schemas.RLock() |
| 113 | + defer schemas.RUnlock() |
| 114 | + d, ok := schemas.d[name] |
| 115 | + if !ok { |
| 116 | + log.Fatalf("No data source schema for %q registered", name) |
| 117 | + } |
| 118 | + return d.Schema |
| 119 | +} |
0 commit comments