|
| 1 | +// Package model defines the generator's internal data model: the in-memory |
| 2 | +// types that flow from the parser, through schema conversion, into the |
| 3 | +// emitter and the run report. These types are deliberately decoupled from |
| 4 | +// both the OpenAPI input format and the Terraform Plugin Framework output |
| 5 | +// |
| 6 | +// The single exception is Spec.Components, which retains a handle to the |
| 7 | +// libopenapi component set so that schemas can be lazily resolved without |
| 8 | +// re-parsing the spec. |
| 9 | +package model |
| 10 | + |
| 11 | +import ( |
| 12 | + v3 "github.com/pb33f/libopenapi/datamodel/high/v3" |
| 13 | +) |
| 14 | + |
| 15 | +// ---------------------------------------------------------------------------- |
| 16 | +// Enumerations |
| 17 | +// |
| 18 | +// Internal-only enums (SchemaKind,GenerationStage) use stable lowercase |
| 19 | +// tokens for debuggability. |
| 20 | +// |
| 21 | +// ---------------------------------------------------------------------------- |
| 22 | + |
| 23 | +// ArtifactKind distinguishes a read-only data source from a full-CRUD resource. |
| 24 | +type ArtifactKind string |
| 25 | + |
| 26 | +const ( |
| 27 | + ArtifactKindResource ArtifactKind = "resource" |
| 28 | + ArtifactKindDataSource ArtifactKind = "data_source" |
| 29 | +) |
| 30 | + |
| 31 | +// SchemaKind classifies a normalized Schema node after allOf flattening and |
| 32 | +// oneOf/anyOf variant detection. |
| 33 | +type SchemaKind string |
| 34 | + |
| 35 | +const ( |
| 36 | + SchemaKindPrimitive SchemaKind = "primitive" |
| 37 | + SchemaKindObject SchemaKind = "object" |
| 38 | + SchemaKindArray SchemaKind = "array" |
| 39 | + SchemaKindMap SchemaKind = "map" |
| 40 | + SchemaKindVariant SchemaKind = "variant" // oneOf / anyOf |
| 41 | + SchemaKindRefCycle SchemaKind = "ref_cycle" |
| 42 | +) |
| 43 | + |
| 44 | +// IdStrategy describes how the Terraform resource ID is derived from the API response. |
| 45 | +type IdStrategy string |
| 46 | + |
| 47 | +const ( |
| 48 | + IdStrategyDataID IdStrategy = "data.id" |
| 49 | + IdStrategyDataAttributesID IdStrategy = "data.attributes.id" |
| 50 | + IdStrategyDataAttributesUID IdStrategy = "data.attributes.uuid" |
| 51 | + IdStrategyHeaderLocation IdStrategy = "header.location" |
| 52 | +) |
| 53 | + |
| 54 | + |
| 55 | +// ---------------------------------------------------------------------------- |
| 56 | +// Parser-facing types |
| 57 | +// ---------------------------------------------------------------------------- |
| 58 | + |
| 59 | +// Spec is the root container, loaded once per tfgen run. |
| 60 | +type Spec struct { |
| 61 | + // Source is the filesystem path to the OpenAPI YAML/JSON |
| 62 | + Source string |
| 63 | + // Operations holds every operation, regardless of tracking-field state, |
| 64 | + // sorted by (path, method) for deterministic iteration. |
| 65 | + Operations []*Operation |
| 66 | + // Components is the shared component set, retained for lazy $ref resolution. |
| 67 | + Components *v3.Components |
| 68 | +} |
| 69 | + |
| 70 | +// Operation is a single OpenAPI operation, tagged with whether it is in scope |
| 71 | +// for generation. |
| 72 | +type Operation struct { |
| 73 | + // Path is the OpenAPI path template, e.g. /api/v2/users/{user_id}. |
| 74 | + Path string |
| 75 | + // Method is the HTTP method (GET/POST/PUT/PATCH/DELETE). |
| 76 | + Method string |
| 77 | + // OperationId is the OpenAPI operationId, used as the SDK method anchor. |
| 78 | + OperationId string |
| 79 | + // Tag is the OpenAPI tag, driving SDK package selection. Must be non-empty |
| 80 | + // when Tracking != nil. |
| 81 | + Tag string |
| 82 | + // Tracking is the decoded tracking-field extension; nil iff the extension |
| 83 | + // is absent. Defined by tracking.go. |
| 84 | + Tracking *TrackingFieldMetadata |
| 85 | + // RequestSchema is the resolved request body schema, if any. |
| 86 | + RequestSchema *Schema |
| 87 | + // ResponseSchema is the resolved 2xx response schema, if any. |
| 88 | + ResponseSchema *Schema |
| 89 | +} |
| 90 | + |
| 91 | +// Schema is a normalized, recursive view of an OpenAPI schema after allOf |
| 92 | +// flattening and oneOf/anyOf variant detection. |
| 93 | +type Schema struct { |
| 94 | + Kind SchemaKind |
| 95 | + // Properties is populated for objects only; iteration is always sorted. |
| 96 | + Properties map[string]*Schema |
| 97 | + // Required is populated for objects only; sorted. |
| 98 | + Required []string |
| 99 | + // Items is populated for arrays only. |
| 100 | + Items *Schema |
| 101 | + // Variants is populated for oneOf/anyOf only. |
| 102 | + Variants []*Schema |
| 103 | + // Type is the primitive type (string/integer/number/boolean). |
| 104 | + Type string |
| 105 | + // Format is the optional OpenAPI format (date-time, int64, ...). |
| 106 | + Format string |
| 107 | + // Enum holds the allowed values, if constrained. |
| 108 | + Enum []string |
| 109 | + // Sensitive is true when the schema is annotated sensitive: true. |
| 110 | + Sensitive bool |
| 111 | +} |
| 112 | + |
| 113 | +// ---------------------------------------------------------------------------- |
| 114 | +// Model / emit types |
| 115 | +// ---------------------------------------------------------------------------- |
| 116 | + |
| 117 | +// Artifact is the internal projection of a flagged Operation, ready for |
| 118 | +// emission. There is one Artifact per (Kind, Name) pair. |
| 119 | +type Artifact struct { |
| 120 | + // Name is the Terraform-facing artifact name (without the datadog_ prefix). |
| 121 | + Name string |
| 122 | + Kind ArtifactKind |
| 123 | + // Schema is the Terraform schema derived from the response (and request, |
| 124 | + // for resources). |
| 125 | + Schema *AttributeTree |
| 126 | + // Lifecycle is set for resources only; data sources carry only a Read. |
| 127 | + Lifecycle *LifecycleBindings |
| 128 | + // SourceFile is the output path, e.g. datadog/fwprovider/<file>.go. |
| 129 | + SourceFile string |
| 130 | +} |
| 131 | + |
| 132 | +// AttributeTree is the root of the Terraform schema tree for one artifact. |
| 133 | +type AttributeTree struct { |
| 134 | + Attributes []*Attribute |
| 135 | +} |
| 136 | + |
| 137 | +// Attribute mirrors a Terraform Plugin Framework attribute or nested block |
| 138 | +// one-to-one. The emitter walks this tree to produce the Schema() method body. |
| 139 | +type Attribute struct { |
| 140 | + // Path is the dot-delimited attribute path, e.g. spec.replicas. It doubles |
| 141 | + // as the per-attribute hook ID anchor. |
| 142 | + Path string |
| 143 | + // TfType is the framework type, e.g. schema.StringAttribute. |
| 144 | + TfType string |
| 145 | + // GoType is the corresponding model-struct type, e.g. types.String. |
| 146 | + GoType string |
| 147 | + |
| 148 | + Required bool |
| 149 | + Optional bool |
| 150 | + Computed bool |
| 151 | + Sensitive bool |
| 152 | + |
| 153 | + // Default is the optional default value, encoded as a Go expression. |
| 154 | + Default *Literal |
| 155 | + // Validators is the fingerprintable validator list for this attribute. |
| 156 | + Validators []ValidatorSpec |
| 157 | + // Description is always populated from the OpenAPI description (repo convention). |
| 158 | + Description string |
| 159 | + // Children holds nested attributes for nested blocks. |
| 160 | + Children []*Attribute |
| 161 | +} |
| 162 | + |
| 163 | +// Literal is a default value rendered as a Go source expression |
| 164 | +// (e.g. `true`, `"foo"`, `int64(3)`). |
| 165 | +type Literal struct { |
| 166 | + GoExpr string |
| 167 | +} |
| 168 | + |
| 169 | +// ValidatorSpec is a deterministic, fingerprintable description of a framework |
| 170 | +// validator: the constructor plus its Go-source-rendered arguments. |
| 171 | +type ValidatorSpec struct { |
| 172 | + // Name is the validator constructor, e.g. stringvalidator.LengthAtLeast. |
| 173 | + Name string |
| 174 | + // Args are the constructor arguments rendered as Go source expressions. |
| 175 | + Args []string |
| 176 | +} |
| 177 | + |
| 178 | +// LifecycleBindings maps each Terraform CRUD method to the SDK call that |
| 179 | +// implements it. Resources only. |
| 180 | +type LifecycleBindings struct { |
| 181 | + Create *SDKCall |
| 182 | + Read *SDKCall |
| 183 | + Update *SDKCall |
| 184 | + Delete *SDKCall |
| 185 | + IdStrategy IdStrategy |
| 186 | +} |
| 187 | + |
| 188 | +// SDKCall is a single datadog-api-client-go invocation plus the mappers that |
| 189 | +// translate to and from the Terraform model. |
| 190 | +type SDKCall struct { |
| 191 | + // OperationId is used to resolve the SDK method via reflection. |
| 192 | + OperationId string |
| 193 | + // RequestMappers populate the request object from the Terraform model. |
| 194 | + RequestMappers []Mapper |
| 195 | + // ResponseMappers populate the Terraform model from the SDK response. |
| 196 | + ResponseMappers []Mapper |
| 197 | +} |
| 198 | + |
| 199 | +// Mapper describes a single field-level translation between the Terraform |
| 200 | +// model and an SDK request/response type. |
| 201 | +type Mapper struct { |
| 202 | + // TfPath is the dotted attribute path in the Terraform model, e.g. spec.replicas. |
| 203 | + TfPath string |
| 204 | + // SdkPath is the corresponding field path on the SDK type. |
| 205 | + SdkPath string |
| 206 | + // GoType is the Go type used at this mapping site, e.g. types.String. |
| 207 | + GoType string |
| 208 | +} |
0 commit comments