Use YAML variables to name keys [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to use a YAML variable to name a YAML key.
I've tried this, but yaml validator complains that its not valid YAML
#Elastic search configuration
variable_key: &variable_key_name vodacom_salimdev_local
fos_elastica:
clients:
default:
host: %fos_elastica_host%
port: %fos_elastica_port%
logger: false
headers: { Authorization: some_security_token }
indexes:
*variable_key_name:
client: default
settings:
index:
analysis:
analyzer:
custom_analyzer :
type : custom
The reason i want a variable name for a key is because, i'll be reading the key from a different file. How do i name my index names using variables ?

Yes you can:
#Elastic search configuration
variable_key: &variable_key_name vodacom_salimdev_local
fos_elastica:
clients:
default:
host: %fos_elastica_host%
port: %fos_elastica_port%
logger: false
headers: { Authorization: some_security_token }
indexes:
%index1%:
client: default
settings:
index:
analysis:
analyzer:
custom_analyzer :
type : custom
Where the index1 var is defined in your parameters.yml or config.yml files. (or any parameters section of your loaded YAML files)
parameters:
index1: my_main_app_es_index_name

Related

Rule governing sequence of fields in a YAML

Checking the validity of a YAML using https://onlineyamltools.com/convert-yaml-to-json
The below YAML is correct
# Valid yaml (field "name" placed at LAST position)
match:
- uri:
prefix: "/mysvc1/"
route:
- destination:
host: myservice1
port:
number: 80
name: "svc1-routes"
However, if I move the field name to first position, the YAML becomes invalid. What is the reason?
# Invalid yaml (field "name" placed at FIRST position)
match:
name: "svc1-routes" # <---- ERROR ----
- uri:
prefix: "/mysvc1/"
route:
- destination:
host: myservice1
port:
number: 80
The error message:
Error: YAMLException: end of the stream or a document separator is expected at line 4, column 1:
- uri:
^
In contrary to your comment, name and match are on the same level because they share the same indentation. name is in no way nested in match (nor is route).
The list items, however, are nested in match since YAML understands the - as parts of the indentation, hence the list items are considered more indented than match and are thus nested in it.
Concerning your error:
name: "svc1-routes"
- uri:
In this part, the mapping key name is assigned the scalar value svc1-routes. Each mapping key may only have one value. On the next line, a sequence starts which is on a deeper indentation level (as explained above) but YAML can't put it anywhere because the key name already has a value. This is why it issues an error.
You can freely switch the mapping keys together with their nested values, e.g.:
route:
- destination:
host: myservice1
port:
number: 80
name: "svc1-routes"
match:
- uri:
prefix: "/mysvc1/"
This will load to the same structure as per YAML spec.

Swagger not matching defenition [duplicate]

This question already has answers here:
Post a json body with swagger
(1 answer)
How to describe this POST JSON request body in OpenAPI (Swagger)?
(3 answers)
Swagger POST with json body
(1 answer)
Closed 5 years ago.
I'm getting the error:
0 $refs cannot match any of the following: "#/definitions",
"#/responses"
When trying to use a definition like this (simplified version):
definitions:
Camera:
type: object
properties:
ip:
description: "device IP address"
type: string
required: [ip]
paths:
/cameras:
put:
parameters:
-
$ref: "#/definitions/Camera"

spring.profiles is not working as expected in spring boot

boot config *.yml file.
server.port: 2222
spring:
application:
name: x-service
data:
mongodb:
host: db.x
database: x
# userName: ${db.userName}
# password: ${db.password}
rabbitmq:
# port: ${queue.port}
host: queue.x
username: ${queue.userName}
password: ${queue.password}
listener:
max-concurrency: 1
prefetch: 1
acknowledge-mode: auto
auto-startup: true
dynamic: true
###########DEV##############
spring.profiles: dev
#queue.virtual.host: xuser
queue.userName: guest
queue.password: guest
queue.port: 5672
#db.userName:
#db.password:
falconUrl: http://x.y.com
##########DEFAULT###########
spring.profiles: qa
queue.virtual.host: xuser
queue.userName: xuser
queue.password: xpassword
queue.port: 3456
db.userName: xuser
db.password: xpassword
falconUrl: http://x.z.com
It gives me org.yaml.snakeyaml.parser.ParserException: while parsing MappingNode
in 'reader', line 1, column 1:
server.port: 2222
^
Duplicate key: spring.profiles
in 'reader', line 47, column 1:
error. If I comment properties of one of the profile.It works fine.
Can anyone please suggest what is wrong here?
The error message is actually quite specific and accurate: in the top-level mapping of your YAML file (the one starting with the key-value pair server.port and 2222 you have two identical keys (the scalar spring.profiles). And duplicate keys are not allowed in YAML, as the are required to be unique according to the specification.
The underlying problem is that if you want to change the configuration depending on the environment, you'll have to follow the documented specification, which states that:
A YAML file is actually a sequence of documents separated by --- lines, and each document is parsed separately to a flattened map.
If a YAML document contains a spring.profiles key, then the profiles value (comma-separated list of profiles) is fed into the Spring Environment.acceptsProfiles() and if any of those profiles is active that document is included in the final merge (otherwise not)
Your YAML file is a single implicit YAML document because it lacks the directive indicator --- that occurs at the beginning of an explicit YAML document. (the YAML directive ... that indicates end-of-document might not be supported properly supported by snake-yaml, at least it is not mentioned in the examples).
Your code should look like:
server.port: 2222
spring:
application:
name: x-service
data:
mongodb:
host: db.x
database: x
# userName: ${db.userName}
# password: ${db.password}
rabbitmq:
# port: ${queue.port}
host: queue.x
username: ${queue.userName}
password: ${queue.password}
listener:
max-concurrency: 1
prefetch: 1
acknowledge-mode: auto
auto-startup: true
dynamic: true
###########DEV##############
---
spring.profiles: dev
#queue.virtual.host: xuser
queue.userName: guest
queue.password: guest
queue.port: 5672
#db.userName:
#db.password:
falconUrl: http://x.y.com
##########DEFAULT###########
---
spring.profiles: qa
queue.virtual.host: xuser
queue.userName: xuser
queue.password: xpassword
queue.port: 3456
db.userName: xuser
db.password: xpassword
falconUrl: http://x.z.com
The statement in the documentation that "each document is parsed separately to a flattened map" is of course only true, if each of the documents has a mapping at the top level. That is what spring-boot expects, but you can as easily have a scalar or sequence at the top level of a document, and such documents are certainly not parsed by snake-yaml to a flattened map.

Create complex types (definitions) in Swagger [duplicate]

This question already has answers here:
Swagger: How to have a property reference a model in OpenAPI 2.0 (i.e. nest the models)?
(2 answers)
Closed 2 years ago.
I created a definition called Product and another called Text (see code).
On parameters of paths I can not use the type Text created in definitions. On the definition Product I have a property called message and I want that property to be the type Text too.
(...)
paths:
/products:
get:
summary: Product Types
description: |
Description text
parameters:
- name: latitude
in: query
description: Latitude component of location.
required: true
### The type Text was not found here
type: Text ### The type Text was not found here
(...)
definitions:
Product:
properties:
message:
### The type Text was not found here
type: Text ### Compilation Error in this line ####
name:
type: string
description: Data description.
Text:
properties:
code:
type: string
But this error occurs:
Swagger Error:
Data does not match any schemas from 'anyOf'.
How can I reference the type Text on the type Product?
Please use $ref instead. Here is an example
type: object
required:
- name
properties:
name:
type: string
address:
$ref: '#/definitions/Address'
age:
type: integer
format: int32
minimum: 0
Ref: https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#simple-model

Access alias properties in Ebean when loading initial test data in Play

I'm loading initial test data for my models in Play 2.1.
I would like to access the String property "question" of Question Model.
Here is my initial-data.yml file:
- &q1 !!models.Question
id: 001
question: Sample question?
- &mem1 !!models.Member
id: 001
memberName: Test
!!models.SecurityQuestion
member: *mem1
question: *q1.question
answer: sample answer
But all I get is the following error:
Test models.ModelsTest.fullTest failed: while scanning an alias; expected alphabetic or numeric character, but found .
How do I access the properties of an alias?
What about this:
questions:
- !!models.Question
id: 001
question: Sample question?
members:
- !!models.Member
id: 001
memberName: Test
security:
!!models.SecurityQuestion
member: !!models.Member
id: 001
question: !!models.Question
id: 001
answer: sample answer

Resources