Set values based on the ip address in JSON with if the else
Using perl JSON module.
I'm trying to setup my etc/config.json so that I never have to edit it when I move code from development->staging->production.
sql ip addresses will be different for different VM's
{
"html_title" : "Bogus",
"local_ip" :
"default" : "[production]",
"hosts" :
{
"production" :
{
"10.11.12.13" : "b102-vm",
"10.11.12.14" : "b103-vm"
},
"stage" :
{
"10.11.12.15" : "b102-vm-s",
"10.11.12.16" : "b103-vm-s"
},
},
"production" :
{
"some_schema" :
{
"host" : "10.11.12.13",
"database" : "some_table",
"dsn" : "DBI:mysql:[production.some_schema.database]:[production.some_schema.host]",
"user" : "user",
"pass" : "pass"
}
},
"stage" :
{
"some_schema" :
{
"host" : "10.11.12.16",
"database" : "some_table",
"dsn" : "DBI:mysql:[stage.some_schema.database]:[stage.some_schema.host]",
"user" : "user",
"pass" : "pass"
}
},
"if" : HERE IS WHERE I GET LOST.
{
"local_ip" : {
"anyOf" : ["[hosts.production]"]
}
},
"then" : { "default" : "[production]" }
"else" :
{
"if" :
{
"local_ip" : {
"anyOf" : ["[hosts.stage]"]
}
},
"then" : { "default" : "[production]" }
}
}
Would like to see "default" point to the production or stage values
As others have pointed out in the comments, JSON is not a programming language; it is a data storage format.
I can think of two obvious ways to do this. Yours is, I'm sorry to say, not one of them.
Both of my solutions involve your various systems having an environment variable set which identifies the kind of environment it is. I'm also going to assume some kind of get_config() function that reads a config file and returns a Perl data structure of the config.
Solution 1: Separate files
Have two files called "staging.json" and "production.json". The get_config() function reads the required one.
sub get_config {
my %valid_env = (
staging => 1,
production => 1,
);
die "$ENV{ENVIRONMENT} is not a valid environment type\n"
unless $valid_env{$ENV{ENVIRONMENT}};
my $json = read_file("$ENV{ENVIRONMENT}.json");
return JSON->new->decode($json);
}
As an extension to this, you could have three files - common.json, staging.json and production.json. You'd always read common.json along with the correct environment file and merge the two data structures into one before returning the data.
Solution 2: Munge the config
Stick with your existing file but munge the data structure after you've read it and before you return it.
sub get_config {
my %valid_env = (
staging => 1,
production => 1,
);
die "$ENV{ENVIRONMENT} is not a valid environment type\n"
unless $valid_env{$ENV{ENVIRONMENT}};
my $json = read_file('config.json');
my $config = JSON->new->decode($json);
# Use $ENV{ENVIRONMENT} to munge the contents of $config
# so it only contains data about the current environment
return $config;
}
Related
I wanted to create an index on a membershipAccount collection, that had a collation rule set that would allow the find method to find a username while being case-insensitive.
[
2,
{
"username" : 1.0
},
"username_1",
"kollecto_prod.membershipAccounts",
{
"locale" : "da",
"caseLevel" : false,
"caseFirst" : "off",
"strength" : 1,
"numericOrdering" : false,
"alternate" : "non-ignorable",
"maxVariable" : "punct",
"normalization" : false,
"backwards" : false,
"version" : "57.1"
}
]
I have a user named - user#test.com in my membershipAccountscollection.
But this is my result of my find queries:
db.getCollection('membershipAccounts').find({username: "user#test.com"}) // 1 result - ok
db.getCollection('membershipAccounts').find({username: "USER#test.com"}) // 0 results - nope
db.getCollection('membershipAccounts').find({username: "USER#TEST.com"}) // 0 results - nope
db.getCollection('membershipAccounts').find({username: "USER#TEST.COM"}) // 0 results - nope
Is my collation incorect or have I misunterstood the concept?
You need to either set the desired collation in the query, or set the default collation on the collection. Creating an index with a collation does not make that collation the default one used in queries.
Per query:
MongoDB Enterprise ruby-driver-rs:PRIMARY> db.foo.insert({v:'test'})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise ruby-driver-rs:PRIMARY> db.foo.find({v:'TEST'}).collation({locale:'en',caseLevel:false,strength:1})
{ "_id" : ObjectId("5ed8302ba6de3bc31c771682"), "v" : "test" }
Default collation:
MongoDB Enterprise ruby-driver-rs:PRIMARY> c={locale:'en',caseLevel:false,strength:1}
{ "locale" : "en", "caseLevel" : false, "strength" : 1 }
MongoDB Enterprise ruby-driver-rs:PRIMARY> db.createCollection('bar',{collation:c})
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1591226649, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1591226649, 1)
}
MongoDB Enterprise ruby-driver-rs:PRIMARY> db.bar.insert({v:'test'})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise ruby-driver-rs:PRIMARY> db.bar.find({v:'TEST'})
{ "_id" : ObjectId("5ed8311fa6de3bc31c771683"), "v" : "test" }
So what I ended up doing instead was in my code as follows:
var account = DBHelper.membershipAccountCollection.Find(
x => x.Username.ToLower().Contains(username)
).FirstOrDefault();
var cu = DBHelper.customerUserCollection.AsQueryable().First(
x => x.Email.ToLower().Contains(model.UserName)
);
This works since I already had the collection object in my C# code, so I could that way utilize the power of C# instead. I realize this is not a general answer to the overall question, but it works in my case. If anyone has a more generalized answer to this question I am more than happy to accept this as an answer at a later date.
Trying to create a new jira ticket of specific requestType, but it is nested two levels deep. Tried few possible alterations, but no luck. Here's the code I have,
require 'jira-ruby' # https://github.com/sumoheavy/jira-ruby
options = {
:username => jira_username,
:password => jira_password,
:site => 'https://jiraurl/rest/api/2/',
:context_path => '',
:auth_type => :basic,
:read_timeout => 120
}
client = JIRA::Client.new(options)
issue = client.Issue.build
fields_options = {
"fields" =>
{
"summary" => "Test ticket creation",
"description" => "Ticket created from Ruby",
"project" => {"key" => "AwesomeProject"},
"issuetype" => {"name" => "Task"},
"priority" => {"name" => "P1"},
"customfield_23070" =>
{
"requestType" => {
"name" => "Awesome Request Type"
}
}
}
}
issue.save(fields_options)
"errors"=>{"customfield_23070"=>"Operation value must be a string"}
Also tried passing a JSON object to customfield_23070,
"customfield_23070": { "requestType": { "name": "Awesome Request Type" } }
still no luck, get the same error message.
If it helps, this is how customfield_23070 looks like in our Jira,
Does anyone know how to set requestType in this case, please? Any help is greatly appreciated!!
It seems that for custom fields with specific data types (string/number), you must pass the value as:
"customfield_1111": 1
or:
"customfield_1111": "string"
instead of:
"customfield_1111":{ "value": 1 }
or:
"customfield_1111":{ "value": "string" }
I'm not sure but you can try this possible examples:
eg.1:
"customfield_23070"=>{"name"=>"requestType","value"=>"Awesome Request Type"}
eg.2:
"customfield_23070"=>{"requestType"=>"Awesome Request Type"}
eg.3:
"customfield_23070"=>{"value"=>"Awesome Request Type"}
eg.4
"customfield_23070"=>{"name"=>"Awesome Request Type"}
for ref there are 2 methods depending upon the fields you are interacting with
have a look here '
updating-an-issue-via-the-jira-rest-apis-6848604
' for the applicable fields for update via verb operations, the other fields you can use examples as per above,
you can use both methods within the same call
{
"update": {"description": [{"set": "Description by API Update - lets do this thing"}]},
"fields": {"customfield_23310": "TESTING0909"}
}
Ok, I think I found how to do it.
You need to provide a string, and that string is the GUID of the RequestType.
In order to get that GUID. You need to run the following in a scriptrunner console:
import com.atlassian.jira.component.ComponentAccessor
def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("ISSUE-400546") //Issue with the desired Request Type
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Tipo de solicitud del cliente") //Change it to the name of your request type field
issue.getCustomFieldValue(cf)
Source: https://community.atlassian.com/t5/Jira-Software-questions/how-to-set-request-type-value-in-while-create-jira-issue/qaq-p/1106696
I have a Json Tree for Teachers database
{ "Teachers":
{
"Teacher_id" : {
"email" : "teacher1#something.com",
"name" : "teacher1",
"Subjects" : {0 : "Maths",
1 : "science"
},
"Subject_Exp-in-years" :{ 0:"Maths_2",
1:"Science_4"
}
},
"Teacher_id" : {
"email" : "teacher2#something.com",
"name" : "teacher2",
"Subjects" : {0 : "Geography",
1 : "science"
},
"Subject_Exp-in-years" :{ "Geography_5",
"Science_1"
}
}
}
How can I get Set of Teachers who teach Science
I have used the query
var db_ref=firebase.database().ref();
db_ref.child("Teachers").orderByChild("Subjects").equalTo("Science")
How can I check if a value is present in an array in the database by querying functions?
When using .orderByChild("Subjects").equalTo("Science") doesn't work because none of the Subjects node children is equal to Science. So in order to solve this, I recomand you to change a little bit your database structure like this:
{ "Teachers":
{
"Teacher_id" : {
"email" : "teacher1#something.com",
"name" : "teacher1",
"Subjects" : {Maths: true,
science: true
},
As you can see i have changed the childrens of Subjects with: Maths: true and science: true. To check if a value is present under a node, just use exists() function on that node.
Hope it helps.
You can use for loop to read each of the element in an array.
When you read them one by one, you can include if else statement to check whether the value that you want existed or not. Let,s say the value is JOHN.
var index;
var arraylist = ["a", "b", "c"];
for (index = 0; index < arraylist.length; ++index) {
if(arraylist[index] = "JOHN") {
console.log(arraylist[index] + "is in the array");
};
}
Situation
In a project I have this code to select data from a table. Please note, it is working, I only don't get the result I expect.
serviceSurveyQuestions.find({
query: {
survey_id: this.survey_id,
user_id: this.$store.state.auth.user.id, //TODO move this to the hook!!
//todo make satus also not equal new
$or: [
{ status_id: process.env.mfp.statusSurveyQuestionStarted },
{ status_id: process.env.mfp.statusSurveyQuestionPlanned }
],
$sort: {
survey_question_question: 1
},
$limit: 150,
$select: [
'survey_question_question',
'survey_question_at',
'survey_question_answer',
'survey_question_details',
'survey_question_source_id',
'survey_question_source_answer_id',
'survey_question_source_user_id',
'survey_question_step',
'survey_question_dep_step',
'id'
]
}
}).then(page => {
this.listSurveyQuestions = page;
});
When I see what would be in one item of listSurveyQuestion I will see this:
{
"survey_question_question": "PEN 10 Scope vaststellen",
"survey_question_at": "2017-06-23T06:46:10.038Z",
"survey_question_answer": "",
"survey_question_details": "tester done",
"survey_question_source_id": 83499707,
"survey_question_source_answer_id": 74864,
"survey_question_source_user_id": 83488216,
"survey_question_step": 10,
"survey_question_dep_step": null,
"id": 4651,
"source_user": {
"user_id": 1005
},
"status": {
"status": "Planned"
},
"language": {
"language": "Dutch"
,
"source": {
"source": "MexonInControl - Pob - Dev (local)"
},
"survey_question": [{
"answer_type_id": 1014,
"answer_en": null,
"answer_nl": null,
"answer_explanation_en": null,
"answer_explanation_nl": null,
"survey_question_next_id": 4652
} ]
}
I know the result is comming from the configuration in my get and find hook of the service being called.
Expected Result
What I expect to happen is that the data returned is only the columns defined in the $SELECT. If I leave this as is, it will work but I'm getting to much data from the database which can be seen later as a security breach. Not with this example, but with other tables it will.
** Question **
So what do I need to change to have this functioning as expected. You could adapt the return of the service, but then I can't use the same service in other situations for the columns aren't available. Or can you pass an option to the service which will result in if (parameter = view 1) then return view 1 and so on.
** Solving **
Remark 1:
So I just see the 'cause' is a bit different. The configured hooks returns more columns from the question table which are not shown. So my guess here is that if you don't configure the includes in the find query, it will pass all includes. I need to check that and if this is the case, see if there is a option to not select the 'includes' as well.
Assuming that the hook you are referring to is setting hook.params.sequelize similar to this answer you will have to check if you included properties are also set in the $select query with something like this:
// GET /my-service?include=1
function (hook) {
const include = [];
const select = hook.params.query.$select;
// Go through all properties that are added via includes
['includeProp1', 'includeProp2'].forEach(propertyName => {
// If no $select or the include property is part of the $select
if(!select || select.indexOf(propertyName) !== -1) {
include.push({ model: ModelForIncludeProp1 });
}
});
hook.params.sequelize = { include };
return Promise.resolve(hook);
}
I have some mongoDb collections 'classrooms' and 'students' of the form:
Classrooms:
{
"_id" : ObjectId("56c7219dbd5f92cd78ae4b7e"),
"name" : "My Students",
"user" : ObjectId("56c7218cbd5f92cd78ae4b7c"),
"updatedAt" : ISODate("2016-02-19T14:07:25.965+0000"),
"createdAt" : ISODate("2016-02-19T14:07:25.965+0000"),
"students" : [
ObjectId("56dbb26cff34aa686c0d9d25"),
ObjectId("56f7c2bf1982aa9219ae8843")
],
"role" : "user",
"allowDelete" : false,
"__v" : NumberInt(0)
}
Students:
{
"_id" : ObjectId("56dbb26cff34aa686c0d9d25"),
"email" : "1989manjari#gmail.com",
"createdBy" : ObjectId("56c7218cbd5f92cd78ae4b7c"),
"classRoom" : ObjectId("56c7219dbd5f92cd78ae4b7e"),
"mentorEmail" : "gauravatbits#gmail.com",
"studentId" : ObjectId("56ced54303b7cb7b0eda9862"),
"status" : true,
"updatedAt" : ISODate("2016-03-11T15:32:36.806+0000"),
"autoAdd" : true,
"createdAt" : ISODate("2016-03-06T04:30:36.073+0000"),
"__v" : NumberInt(0)
}
My query is:
id_list = db.classrooms.distinct("students");
db.students.find({_id: {$in: id_list}, studentId:{$exists:false}},{email:1, mentorEmail: 1}).pretty()
Now I want to create an endpoint in node api for this response. So i wanted to know how can I write these queries in Mongoose and create endpoint like: app.get('/api/myquery') to get the json result.
P.S. : Is it possible to do this, without creating schema in Mongoose because i also have some collections which have large no. of fields(38 fields). I just want to get some json data by applying queries in already existing collections.
something like this with mongoose
just pseudo code
var model = mongoose.model("collection", mongooseSchema)
app.get('/api/myquery', function(){
model.find({_id : id_list}, "email mentorEmail", function(err, data){
if(err)throw err;
res.json(data) // maybe use JSON.stringify() if didnt get json but that sets application/json in header of HTTP
})
})
You can directly insert jsons in mongoose but you should keep in mind that only that data will be read which are mentioned in the models in mongoose. Do keeping 38 fields is not a problem just insert in directly.