Skip to content

Can't use component position from template in libraryΒ #224

@Zorin95670

Description

@Zorin95670

Describe the bug

When we put the position of component(s) in the library template. After using this component template in leto-modelizer position are not retrieve from template configuration, due to id are generate in component template file and not share with the configuration of the position.

Potential solution

Create a map of id link by a key:

const ids = new Map();

In template use this map to store id associated of the key:

# serverX is the key associated to the new id
resource "server" "{{ generateId('serverX', 'resource_')}}" { 
  (...)
}

Then in the position configuration you can reuse it with getId function:

{
  "terrator-plugin": {
    "{{getId(serverX)}}": {
      "x": 30,
      "y": 30,
      "width": 110,
      "height": 80,
      "needsResizing": false,
      "needsPositioning": false
    },
  }
}

So in Leto-modelizer you have to modify two functions in src/composables/TemplateManager.js:

  • getTemplateFiles
  • generateTemplate
export function generateTemplate(ids, content) {
  return nunjucks.renderString(content, {
    generateId: (key, prefix) => {
      const id = `${prefix || ''}${randomHexString(6)}`;
      ids.set(key, id);
      return id;
    },
    getId: (key) => ids.get(key),
  });
}

(...)

export async function getTemplateFiles(path, templateDefinition) {
  const ids = new Map();

  return Promise.all(
    templateDefinition.files.map(
      (file) => getTemplateFileByPath(`templates/${templateDefinition.key}/${file}`)
        .then(({ data }) => new FileInput({
          path: `${path}/${file}`,
          content: generateTemplate(ids, data),
        })),
    ),
  );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working

    Type

    No type

    Projects

    Status

    πŸ”– Ready

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions