Created
May 20, 2024 17:22
-
-
Save rssnyder/e7df3951808cb60804e93d9b8ae3c2c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# setup | |
set the following env vars: | |
- HARNESS_ACCOUNT_ID: account id | |
- HARNESS_ORG_ID: org id | |
- HARNESS_PLATFORM_API_KEY: harness api key | |
# usage | |
``` | |
python main.py <csv> <project id> <env id> <target group id> | |
``` | |
``` | |
python main.py rules.csv my_test_project dev accountids | |
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import argv, exit | |
from csv import reader | |
from os import getenv | |
from requests import post | |
def create_target_group(project: str, environment: str, name: str, rules: str): | |
resp = post( | |
"https://app.harness.io/cf/admin/segments", | |
params={ | |
"accountIdentifier": getenv("HARNESS_ACCOUNT_ID"), | |
"orgIdentifier": getenv("HARNESS_ORG_ID"), | |
}, | |
headers={"x-api-key": getenv("HARNESS_PLATFORM_API_KEY")}, | |
json={ | |
"environment": environment, | |
"identifier": name, | |
"name": name, | |
"project": project, | |
"rules": rules, | |
}, | |
) | |
resp.raise_for_status() | |
return resp.status_code | |
if __name__ == "__main__": | |
if len(argv) < 5: | |
print(f"usage: {argv[0]} <csv file> <project id> <env id> <target group name>") | |
exit(1) | |
csv_file = argv[1] | |
project = argv[2] | |
env = argv[3] | |
target_group_name = argv[4] | |
rules = [] | |
with open(csv_file) as csv_file: | |
csv_file_reader = reader(csv_file) | |
for row in csv_file_reader: | |
rules.append( | |
{"attribute": row[0], "negate": False, "op": row[1], "values": [row[2]]} | |
) | |
print(create_target_group(project, env, target_group_name, rules)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
requests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
accountID | equal | 012 | |
---|---|---|---|
accountID | equal | 345 | |
accountID | equal | 678 | |
accountID | equal | 910 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment