Skip to content

Commit

Permalink
Rename all Resouce prefixed base properties to Meta., Sorry
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Feb 21, 2024
1 parent 9def1b3 commit 68281b8
Show file tree
Hide file tree
Showing 32 changed files with 297 additions and 272 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Resources to be parsed are defined as Go structs that implement the Resource int
type Config struct {
// For a resource to be parsed by HCLConfig it needs to embed the ResourceInfo type and
// add the methods from the `Resource` interface
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`

ID string `hcl:"id"`

Expand Down Expand Up @@ -104,7 +104,7 @@ func (t *Config) Parse() error {
type PostgreSQL struct {
// For a resource to be parsed by HCLConfig it needs to embed the ResourceInfo type and
// add the methods from the `Resource` interface
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`

Location string `hcl:"location"`
Port int `hcl:"port"`
Expand Down Expand Up @@ -222,7 +222,7 @@ fmt.Println("db_connection_string", c.db_connection_string) // = postgresql://ad
To create types that can be converted from HCL your top level resource needs to embed the
following type into your structs.

``types.ResourceMetadata `hcl:",remain"` ``
``types.ResourceBase `hcl:",remain"` ``

The struct tag `` `hcl:",remain"` ``, must be included with this type as it tells the HCL parser
to unfold the default properties such as `disabled` and `depends_on` from your custom type.
Expand All @@ -238,7 +238,7 @@ the `PostgresSQL` struct will result in a parser error.
type PostgreSQL struct {
// For a resource to be parsed by HCLConfig it needs to embed the ResourceInfo type and
// add the methods from the `Resource` interface
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`

Location string `hcl:"location"`
}
Expand All @@ -252,7 +252,7 @@ the previous example has been modified to make `location` optional.
type PostgreSQL struct {
// For a resource to be parsed by HCLConfig it needs to embed the ResourceInfo type and
// add the methods from the `Resource` interface
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`

Location string `hcl:"location,optional"`
}
Expand All @@ -275,7 +275,7 @@ This can be seen in the following code sample.

```go
type Config struct {
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`

DBConnectionString string `hcl:"db_connection_string"`

Expand All @@ -297,7 +297,7 @@ resource "config" "myconfig" {
}
```

The `Timeout` type used by the field `Timeout` does not need to embed `ResourceMetadata`
The `Timeout` type used by the field `Timeout` does not need to embed `ResourceBase`
as it is not a top level resource but all other struct tags that define blocks and
optional parameters are required.

Expand All @@ -307,7 +307,7 @@ To make child blocks optional you simply need to change the Field type to a refe

```go
type Config struct {
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`

DBConnectionString string `hcl:"db_connection_string"`

Expand All @@ -334,7 +334,7 @@ slice.

```go
type Config struct {
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`

DBConnectionString string `hcl:"db_connection_string"`

Expand Down Expand Up @@ -373,10 +373,10 @@ resource.

```go
type Config struct {
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`

// Other structs can be referenced by defining the type
// to the other struct, the referenced type must implemented types.ResourceMetadata
// to the other struct, the referenced type must implemented types.ResourceBase
MainDBConnection PostgreSQL `hcl:"main_db_connection"`

// It is also possible to reference arrays of structs
Expand All @@ -391,7 +391,7 @@ type Config struct {
type PostgreSQL struct {
// For a resource to be parsed by HCLConfig it needs to embed the ResourceInfo type and
// add the methods from the `Resource` interface
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`

Location string `hcl:"location,optional"`
}
Expand Down Expand Up @@ -901,7 +901,7 @@ to compute the value of the attribute `connection_string`.
type PostgreSQL struct {
// For a resource to be parsed by HCLConfig it needs to embed the ResourceInfo type and
// add the methods from the `Resource` interface
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`
Location string `hcl:"location"`
Port int `hcl:"port"`
Expand Down Expand Up @@ -930,7 +930,7 @@ For example, given the following custom resources
type Config struct {
// For a resource to be parsed by HCLConfig it needs to embed the ResourceInfo type and
// add the methods from the `Resource` interface
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`

ID string `hcl:"id"`

Expand All @@ -955,7 +955,7 @@ func (t *Config) Process() error {
type PostgreSQL struct {
// For a resource to be parsed by HCLConfig it needs to embed the ResourceInfo type and
// add the methods from the `Resource` interface
types.ResourceMetadata `hcl:",remain"`
types.ResourceBase `hcl:",remain"`

Location string `hcl:"location"`
Port int `hcl:"port"`
Expand Down
42 changes: 21 additions & 21 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func (c *Config) findResource(path string) (types.Resource, error) {
// this is an internal error and should not happen unless there is an issue with a provider
// there was, hence why we are here
if c.Resources == nil {
return nil, fmt.Errorf("unable to find resources, reference to parent config does not exist. Ensure that the object has been added to the config: `config.ResourceInfo.AddChild(type)`")
return nil, fmt.Errorf("unable to find resources, reference to parent config does not exist. Ensure that the object has been added to the config: `config.Info.AddChild(type)`")
}

for _, r := range c.Resources {
if r.Metadata().ResourceModule == fqdn.Module &&
r.Metadata().ResourceType == fqdn.Type &&
r.Metadata().ResourceName == fqdn.Resource {
if r.Metadata().Module == fqdn.Module &&
r.Metadata().Type == fqdn.Type &&
r.Metadata().Name == fqdn.Resource {
return r, nil
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ func (c *Config) FindResourcesByType(t string) ([]types.Resource, error) {
res := []types.Resource{}

for _, r := range c.Resources {
if r.Metadata().ResourceType == t {
if r.Metadata().Type == t {
res = append(res, r)
}
}
Expand Down Expand Up @@ -173,11 +173,11 @@ func (c *Config) FindModuleResources(module string, includeSubModules bool) ([]t

for _, r := range c.Resources {
match := false
if includeSubModules && strings.HasPrefix(r.Metadata().ResourceModule, moduleString) {
if includeSubModules && strings.HasPrefix(r.Metadata().Module, moduleString) {
match = true
}

if !includeSubModules && r.Metadata().ResourceModule == moduleString {
if !includeSubModules && r.Metadata().Module == moduleString {
match = true
}

Expand Down Expand Up @@ -277,7 +277,7 @@ func (c *Config) Diff(o *Config) (*ResourceDiff, error) {

for _, r := range o.Resources {
// does the resource exist
cr, err := c.findResource(r.Metadata().ResourceID)
cr, err := c.findResource(r.Metadata().ID)

// check if the resource has been found
if err != nil {
Expand All @@ -287,13 +287,13 @@ func (c *Config) Diff(o *Config) (*ResourceDiff, error) {
}

// check if the resource has changed
if cr.Metadata().ResourceChecksum.Parsed != r.Metadata().ResourceChecksum.Parsed {
if cr.Metadata().Checksum.Parsed != r.Metadata().Checksum.Parsed {
// resource has changes rebuild
parseChanged = append(parseChanged, r)
continue
}

if cr.Metadata().ResourceChecksum.Processed != r.Metadata().ResourceChecksum.Processed {
if cr.Metadata().Checksum.Processed != r.Metadata().Checksum.Processed {
// resource has changes rebuild
processChanged = append(processChanged, r)
continue
Expand All @@ -305,7 +305,7 @@ func (c *Config) Diff(o *Config) (*ResourceDiff, error) {
for _, r := range c.Resources {
found := false
for _, r2 := range o.Resources {
if r.Metadata().ResourceID == r2.Metadata().ResourceID {
if r.Metadata().ID == r2.Metadata().ID {
found = true
break
}
Expand All @@ -320,28 +320,28 @@ func (c *Config) Diff(o *Config) (*ResourceDiff, error) {
for _, r := range c.Resources {
found := false
for _, r2 := range new {
if r.Metadata().ResourceID == r2.Metadata().ResourceID {
if r.Metadata().ID == r2.Metadata().ID {
found = true
break
}
}

for _, r2 := range parseChanged {
if r.Metadata().ResourceID == r2.Metadata().ResourceID {
if r.Metadata().ID == r2.Metadata().ID {
found = true
break
}
}

for _, r2 := range processChanged {
if r.Metadata().ResourceID == r2.Metadata().ResourceID {
if r.Metadata().ID == r2.Metadata().ID {
found = true
break
}
}

for _, r2 := range removed {
if r.Metadata().ResourceID == r2.Metadata().ResourceID {
if r.Metadata().ID == r2.Metadata().ID {
found = true
break
}
Expand All @@ -368,9 +368,9 @@ func (c *Config) RemoveResource(rf types.Resource) error {

pos := -1
for i, r := range c.Resources {
if rf.Metadata().ResourceName == r.Metadata().ResourceName &&
rf.Metadata().ResourceType == r.Metadata().ResourceType &&
rf.Metadata().ResourceModule == r.Metadata().ResourceModule {
if rf.Metadata().Name == r.Metadata().Name &&
rf.Metadata().Type == r.Metadata().Type &&
rf.Metadata().Module == r.Metadata().Module {
pos = i
break
}
Expand Down Expand Up @@ -420,7 +420,7 @@ func (c *Config) Walk(wf WalkCallback, reverse bool) error {
}

// if this is the root module or is disabled skip
if (r.Metadata().ResourceType == types.TypeRoot || r.Metadata().ResourceType == types.TypeModule) || r.Metadata().Disabled {
if (r.Metadata().Type == types.TypeRoot || r.Metadata().Type == types.TypeModule) || r.GetDisabled() {
return nil
}

Expand Down Expand Up @@ -496,11 +496,11 @@ func (c *Config) addResource(r types.Resource, ctx *hcl.EvalContext, b *hclsynta
fqdn := types.FQDNFromResource(r)

// set the ID
r.Metadata().ResourceID = fqdn.String()
r.Metadata().ID = fqdn.String()

rf, err := c.findResource(fqdn.String())
if err == nil && rf != nil {
return ResourceExistsError{r.Metadata().ResourceName}
return ResourceExistsError{r.Metadata().Name}
}

c.Resources = append(c.Resources, r)
Expand Down

0 comments on commit 68281b8

Please sign in to comment.