Create N number of GCP compute instances using Rest api - java

How to create n number of instances in GCP using rest api.
in AWS java SDK, there is a method withMaxCount where we specify number of ec2 instances.
Similarly is there anything for GCP compute.

You can use REST API in loop to create instances.
Example request will look something like this:
{
"kind": "compute#instance",
"name": "INSTANCE-NAME",
"zone": "projects/PROJECT-NAME/zones/us-central1-a",
"machineType": "projects/PROJECT-NAME/zones/us-central1-a/machineTypes/e2-medium",
"displayDevice": {
"enableDisplay": false
},
"metadata": {
"kind": "compute#metadata",
"items": []
},
"tags": {
"items": []
},
"disks": [
{
"kind": "compute#attachedDisk",
"type": "PERSISTENT",
"boot": true,
"mode": "READ_WRITE",
"autoDelete": true,
"deviceName": "INSTANCE-NAME",
"initializeParams": {
"sourceImage": "projects/debian-cloud/global/images/debian-10-buster-v20210122",
"diskType": "projects/PROJECT-NAME/zones/us-central1-a/diskTypes/pd-standard",
"diskSizeGb": "10",
"labels": {}
},
"diskEncryptionKey": {}
}
],
"canIpForward": false,
"networkInterfaces": [
{
"kind": "compute#networkInterface",
"subnetwork": "regions/us-central1/subnetworks/default",
"accessConfigs": [
{
"kind": "compute#accessConfig",
"name": "External NAT",
"type": "ONE_TO_ONE_NAT",
"networkTier": "PREMIUM"
}
],
"aliasIpRanges": []
}
],
"description": "",
"labels": {},
"scheduling": {
"preemptible": false,
"onHostMaintenance": "MIGRATE",
"automaticRestart": true,
"nodeAffinities": []
},
"deletionProtection": false,
"reservationAffinity": {
"consumeReservationType": "ANY_RESERVATION"
},
"serviceAccounts": [
{
"email": "111111111111-compute#developer.gserviceaccount.com",
"scopes": [
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring.write",
"https://www.googleapis.com/auth/servicecontrol",
"https://www.googleapis.com/auth/service.management.readonly",
"https://www.googleapis.com/auth/trace.append"
]
}
],
"shieldedInstanceConfig": {
"enableSecureBoot": false,
"enableVtpm": true,
"enableIntegrityMonitoring": true
},
"confidentialInstanceConfig": {
"enableConfidentialCompute": false
}
}
Replace it with your own project name and service account, and you can test it here.

Related

Keycloak -18.0.1 issue with authorization and multiple polices/permissions

we are getting issue with multiple policies/permission.
Policy A - user A with scope A,B,C works fine.
Policy B - user B with scope B,C,D gives 403.
Policy C - user C with scope C,D,E gives 403.
If User B is added to Policy A it works fine and retunes combined result of scope A,B,C,D.
For User C we need to this add this user to both policy A,B and it works fine and retunes combined result of scope A,B,C,D,E.
We want this to work independently. For user B and C to work by adding them to policy B for user B and policy C for user C.
Both Policies are Positive and Both Permissions are Affirmative.
In Evaluate page it shows as expected but from Postman it gives error.
JSON from Authorization Export
{
"allowRemoteResourceManagement": false,
"policyEnforcementMode": "ENFORCING",
"resources": [
{
"name": "ResourceA",
"type": "urn:generic-rest-api:resources:employees",
"ownerManagedAccess": false,
"attributes": {},
"_id": "1b41c828-b78f-44da-84ab-e62a0b4843b5",
"uris": [
"/v1/employees/*"
],
"scopes": [
{
"name": "urn:generic-rest-api:scopes:employees:attribute:admindescription:view"
},
{
"name": "urn:generic-rest-api:scopes:employees:attribute:accessRole:view"
},
{
"name": "urn:generic-rest-api:scopes:employees:attribute:address:view"
}
]
},
{
"name": "ResourceB",
"type": "urn:generic-rest-api:resources:employees",
"ownerManagedAccess": false,
"displayName": "ResourceB",
"attributes": {},
"_id": "6237e427-55ef-4c1e-9e3d-714e5bfe31c9",
"uris": [
"/v1/employees/*"
],
"scopes": [
{
"name": "urn:generic-rest-api:scopes:employees:attribute:admindescription:view"
},
{
"name": "urn:generic-rest-api:scopes:employees:attribute:companyid:view"
},
{
"name": "urn:generic-rest-api:scopes:employees:attribute:authorizedAmount:view"
}
]
},
{
"name": "Default Resource",
"type": "urn:employee-rest-api:resources:default",
"ownerManagedAccess": false,
"attributes": {},
"_id": "fb476666-f779-4a86-8234-d49e897f6405",
"uris": [
"/*"
]
}
],
"policies": [
{
"id": "1e08f5f3-a766-416b-9448-50716dd0580b",
"name": "PolicyB",
"type": "user",
"logic": "POSITIVE",
"decisionStrategy": "UNANIMOUS",
"config": {
"users": "[\"UserB\"]"
}
},
{
"id": "8f90f109-0c7b-4caa-9620-50b7d36bd463",
"name": "Default Policy",
"description": "A policy that grants access only for users within this realm",
"type": "js",
"logic": "POSITIVE",
"decisionStrategy": "AFFIRMATIVE",
"config": {
"code": "// by default, grants any permission associated with this policy\n$evaluation.grant();\n"
}
},
{
"id": "c4ce02d8-cb2d-479f-ad22-82f092535b1b",
"name": "PolicyA",
"type": "user",
"logic": "POSITIVE",
"decisionStrategy": "UNANIMOUS",
"config": {
"users": "[\"UserA\"]"
}
},
{
"id": "29f9ae37-afa9-4a98-864f-bc9f8dd7a783",
"name": "PermissionA",
"type": "resource",
"logic": "POSITIVE",
"decisionStrategy": "AFFIRMATIVE",
"config": {
"resources": "[\"ResourceA\"]",
"applyPolicies": "[\"PolicyA\"]"
}
},
{
"id": "3b71972f-3754-464d-99b6-f5c11e5962cc",
"name": "PermissionB",
"type": "resource",
"logic": "POSITIVE",
"decisionStrategy": "AFFIRMATIVE",
"config": {
"resources": "[\"ResourceB\"]",
"applyPolicies": "[\"PolicyB\"]"
}
},
{
"id": "3f21cf9d-f0b4-4b54-a3b9-5cbc4c1f060c",
"name": "Default Permission",
"description": "A permission that applies to the default resource type",
"type": "resource",
"logic": "POSITIVE",
"decisionStrategy": "AFFIRMATIVE",
"config": {
"defaultResourceType": "urn:employee-rest-api:resources:default",
"applyPolicies": "[\"Default Policy\"]"
}
}
],
"scopes": [
{
"id": "c14170f6-0ef7-416e-bde8-c394f6f3ed13",
"name": "urn:generic-rest-api:scopes:employees:attribute:admindescription:view"
},
{
"id": "1d88b211-7961-4a2b-bfae-696566366f1f",
"name": "urn:generic-rest-api:scopes:employees:attribute:adusertype:view"
},
{
"id": "bfa6a52a-46de-4e86-8c72-4ea738752cd0",
"name": "urn:generic-rest-api:scopes:employees:attribute:accessRole:view"
},
{
"id": "f646b5af-bcc4-42a5-b135-de5d0c78ac5b",
"name": "urn:generic-rest-api:scopes:employees:attribute:address:view"
},
{
"id": "50286c2a-b0b4-423b-ac99-9b7b64bdebee",
"name": "urn:generic-rest-api:scopes:employees:attribute:authorizedAmount:view"
},
{
"id": "08140d1b-1c51-4563-96c4-4f6a628e9933",
"name": "urn:generic-rest-api:scopes:employees:attribute:companyid:view"
}
],
"decisionStrategy": "UNANIMOUS"
}

Problem with checking values of JSON array using Rest Assured

I'm trying to add some tests with Rest-Assured to my application, but I can't figure out how to assert some nested values. The error message is :
Expected: (a collection containing "json")
Actual: [[json, spring, gulp, path etc...]]
Here is the code :
when().
get("/api/personsByID/{id}/{count}", 262, 2).
then().
statusCode(200).
body("personDependencies.name", hasItems("json"));
And here is the JSON file that is returned by rest controller:
[
{
"id": 346,
"verified": true,
"displayName": "eda656a2c3cb59ae840e40a28ba4ab50bfb9de0185abcb901c6af6dc59d6668f",
"emails": [
{
"email": "16a23f2e5477df0bbcad718c3abc235b2cb8a1b6648d14f58d42a7be13df2b6e"
}
],
"personDependencies": [
{
"name": "json"
},
{
"name": "spring"
},
{
"name": "gulp"
},
{
"name": "path"
},
{
"name": "junit"
},
{
"name": "activemq"
},
{
"name": "hibernate"
},
{
"name": "jstl"
},
{
"name": "phantomjs"
},
{
"name": "activiti"
},
{
"name": "commons"
},
{
"name": "h2"
},
{
"name": "joda"
},
{
"name": "log4j"
},
{
"name": "exec"
},
{
"name": "admin"
},
{
"name": "coveralls"
},
{
"name": "cxf"
},
{
"name": "cglib"
},
{
"name": "camel"
},
{
"name": "sugaronrest"
},
{
"name": "tslint"
},
{
"name": "httpclient"
},
{
"name": "guava"
},
{
"name": "inventory"
},
{
"name": "jackson"
},
{
"name": "gson"
},
{
"name": "event"
},
{
"name": "OTRS"
},
{
"name": "maven"
},
{
"name": "karma"
},
{
"name": "slf4j"
},
{
"name": "postgresql"
},
{
"name": "typescript"
},
{
"name": "jasmine"
},
{
"name": "spa"
},
{
"name": "javax.servlet"
}
],
"countries": [],
"member_of": [],
"projects": [],
"employee_type": [],
"languages": [
{
"language": "reStructuredText",
"sum": 575
},
{
"language": "JSON",
"sum": 21
},
{
"language": "JavaScript",
"sum": 4467
},
{
"language": "Java",
"sum": 7958
},
{
"language": "Python",
"sum": 2
},
{
"language": "XML",
"sum": 477
},
{
"language": "Plain Text",
"sum": 41
}
],
"distance": 0.6028837702084446
}
]
I have no idea how to make proper assertions, any help would be great. Thanks!
If I am reading your question right you need to check if a certain values are present in a list that is returned for a particular ID
The below should work for you
given().when().get().then().body("find {it.id == 346}.personDependencies.name", hasItems("json", "jackson"));
The first problem you don't need to check the presence of an item with hasItems, you should use hasItem
when().
get("/api/personsByID/{id}/{count}", 262, 2).
then().
statusCode(200).
body("personDependencies.name", hasItem("json"));
Then if you need to add more message to the assertion when the test fails you can do such way:
when().
get("/api/personsByID/{id}/{count}", 262, 2).
then().
statusCode(200).
body("personDependencies.name", describedAs("Array not containing the provided item",hasItem("json")));
In your case you can validate such a way:
when().
get("/api/personsByID/{id}/{count}", 262, 2).
then().
statusCode(200).
body("personDependencies[*].name", hasItem("json"));

Is there an easy way to flatten a json:api response on Android?

I'm trying to flatten the below response without having to parse it into a class. The reason for this is that the server could add or remove fields at anytime so it needs to be dynamic. We also have another service that returns lookup paths that we use to get data out of the flattened response - like "$.detail.att_one" There is a library for iOS that does the exact thing I'm looking for but as far as I can find nothing similar for Android: https://github.com/infinum/Japx
{
"data": [
{
"type": "items",
"id": "14",
"attributes": {
"item_type": "shape_circle",
"code": null,
"size": "70"
},
"relationships": {
"detail": {
"data": {
"type": "circle",
"id": "90"
}
},
"metadata": {
"data": "metadata"
}
},
"links": {
"self": "http://url/item/14"
}
}
],
"included": [
{
"type": "circle",
"id": "90",
"attributes": {
"att_one": 4,
"att_two": "11111111111",
"att_three": "Bob"
}
}
]}
The result I'm looking for:
{
"data": [
{
"id": "14",
"type": "items",
"item_type": "shape_circle",
"code": null,
"size": "70",
"metadata": {
"data": "metadata"
},
"detail": {
"type": "circle",
"id": "90",
"att_one": 4,
"att_two": "11111111111",
"att_three": "Bob"
},
"links": {
"self": "http://url/item/14"
}
}
]}
There is a nice JSONAPI library that does the thing, but you have to define resource classes for it.
Check out jasminb/jsonapi-converter
It recursively flattens all the included relationships and handles inheritance.

Using Groovy collection to exactly match the string format

I have a question about matching the exact string format using the groovy collection.
def createPullRequest(projectSlug, repoSlug, title, description, sourceBranch, targetBranch) {
//this is reading in the array with the user names
def names = BitbutkcetUtil.getGroupUsers(teamName, activeOnly)
def prResponse = this.steps.httpRequest(
acceptType: 'APPLICATION_JSON',
authentication: this.userId,
contentType: 'APPLICATION_JSON',
httpMode: 'POST',
ignoreSslErrors: true,
quiet: true,
requestBody: """
{
"title": "${title}",
"description": "${description}",
"state": "OPEN",
"open": true,
"closed": false,
"fromRef": { "id": "${sourceBranch}" },
"toRef": { "id": "${targetBranch}" },
"locked": false,
"reviewers": [
//I want to replace this hardcoded names with the string values inside the array `names`
{ "user": { "name": "HardCoded1" } },
{ "user": { "name": "HardCoded2" } },
{ "user": { "name": "HardCoded3" } },
{ "user": { "name": "HardCoded4" } }
]
}
""",
responseHandle: 'STRING',
url: "https://bitbucket.absolute.com/rest/api/latest/projects/${projectSlug}/repos/${repoSlug}/pull-requests",
validResponseCodes: '200:299')
def pullRequest = this.steps.readJSON(text: prResponse.content)
prResponse.close()
return pullRequest['id']
}
What I want to do is I want to replace the hardcoded names inside the reviewers with the string elements inside the array names. I want to use the collection but I have to match the exact format
{ "user": { "name": "HardCoded1" } },
{ "user": { "name": "HardCoded2" } },
{ "user": { "name": "HardCoded3" } },
{ "user": { "name": "HardCoded4" } }
Right now, I have [reviewers: names.collect{ [user: [name: it]] }], and it is outputting the following:
[reviewers:[[user:[name:name1]],
[user:[name:name2]],
[user:[name:name3]],
[user:[name:name4]]]]
How can I make it so that the output is in the following format?
"reviewers": [
//I want to replace this hardcoded names with the string values inside the array `names`
{ "user": { "name": "HardCoded1" } },
{ "user": { "name": "HardCoded2" } },
{ "user": { "name": "HardCoded3" } },
{ "user": { "name": "HardCoded4" } }
]
Any help would be greatly appreciated!
What you see is a result of calling toString() method on a list of map elements. For a valid JSON representation, you may pass a result of the collect() method to a JsonOutput.toJSON(). Something like this:
requestBody: """
{
"title": "${title}",
"description": "${description}",
"state": "OPEN",
"open": true,
"closed": false,
"fromRef": { "id": "${sourceBranch}" },
"toRef": { "id": "${targetBranch}" },
"locked": false,
"reviewers": ${JsonOutput.toJson(names.collect{ [user: [name: it]] })}
}
"""
JsonOutput.toJSON() may need script approval when it gets used for the first time in Jenkins Pipeline.

Elasticsearch Query String Query not working with synonym analyzer

I am trying to configure elastic search with synonyms.
These are my settings:
"analysis": {
"analyzer": {
"category_synonym": {
"tokenizer": "whitespace",
"filter": [
"synonym_filter"
]
}
},
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms_path": "synonyms.txt"
}
}
}
Mappings config:
"category": {
"properties": {
"name": {
"type":"string",
"search_analyzer" : "category_synonym",
"index_analyzer" : "standard",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
And the list of my synonyms
film => video,
ooh => panels , poster,
commercial => advertisement,
print => magazine
I must say that I am using Elasticsearch Java API.
I am using QueryBuilders.queryStringQuery because this is the only way how I set analyzers to my request.
So, when I am making:
QueryBuilders.queryStringQuery("name:film").analyzer(analyzer)
It returns me
[
{
"id": 71,
"name": "Pitch video",
"description": "... ",
"parent": null
},
{
"id": 25,
"name": "Video",
"description": "... ",
"parent": null
}
]
That is perfect for me, but when I am calling something like this
QueryBuilders.queryStringQuery("name:vid").analyzer(analyzer)
I expect that it should return same objects, but there is nothing: []
So, I added asterisk to queryStringQuery:
QueryBuilders.queryStringQuery("name:vid*").analyzer(analyzer)
Works well, but now
QueryBuilders.queryStringQuery("name:film*").analyzer(analyzer)
returns me []
So, how can I configure my elastic search that it will return same objects when I am searching video, vid, film and fil?
Thanks in advance!
Hm, I don't think Elasticsearch will know to "translate" fil into vid :-). So, I think you need edgeNGrams for this, both at indexing and search time.
PUT test
{
"settings": {
"analysis": {
"analyzer": {
"category_synonym": {
"tokenizer": "whitespace",
"filter": [
"synonym_filter",
"my_edgeNGram_filter"
]
},
"standard_edgeNGram": {
"tokenizer": "standard",
"filter": [
"lowercase",
"synonym_filter",
"my_edgeNGram_filter"
]
}
},
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms_path": "synonyms.txt"
},
"my_edgeNGram_filter": {
"type": "edgeNGram",
"min_gram": 2,
"max_gram": 8
}
}
}
},
"mappings": {
"test": {
"properties": {
"name": {
"type": "string",
"analyzer": "category_synonym",
"index_analyzer": "standard_edgeNGram",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
POST test/test/1
{"name": "Pitch video"}
POST test/test/2
{"name": "Video"}
GET /test/test/_search
{
"query": {
"query_string": {
"query": "name:fil"
}
}
}

Categories

Resources