Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaces addEnvVariable mutation with addOrUpdateEnvVariableByName #72

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions api/plugins/action/env_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def run(self, tmp=None, task_vars=None):
value = self._task.args.get('value', None)
scope = self._task.args.get('scope', None)
replace_existing = self._task.args.get('replace_existing', False)

# These, used to add and update, are filled in below
lagoon_project_name = None
lagoon_environment_name = None

self.createClient(task_vars)

Expand All @@ -50,18 +54,20 @@ def run(self, tmp=None, task_vars=None):
type_id = lagoonProject.projects[0]['id']
env_vars = lagoonProject.projects[0]['envVariables']
self._display.v("Project variables: %s" % env_vars)
lagoon_project_name = type_name
elif (type == 'ENVIRONMENT'):
lagoonEnvironment.byNs(
type_name, ['id', 'kubernetesNamespaceName'])
type_name, ['id', 'name', 'kubernetesNamespaceName', "project {id, name}"])
if not len(lagoonEnvironment.environments):
raise AnsibleError("Environment not found.")

lagoonEnvironment.withVariables()
self._display.v(f"environment: {lagoonEnvironment.environments[0]}")
type_id = lagoonEnvironment.environments[0]['id']
env_vars = lagoonEnvironment.environments[0]['envVariables']
lagoon_environment_name = lagoonEnvironment.environments[0]['name']
lagoon_project_name = lagoonEnvironment.environments[0]['project']['name']
self._display.v("Environment variables: %s" % env_vars)

if env_vars == None:
raise AnsibleError(
"Incorrect variable type: %s. Should be PROJECT or ENVIRONMENT." % type)
Expand Down Expand Up @@ -107,13 +113,10 @@ def run(self, tmp=None, task_vars=None):
result['id'] = existing_var['id']
return result

# Delete before recreating.
lagoonVariable.delete(existing_var['id'])

if state == 'absent':
return result

result['data'] = lagoonVariable.add(type, type_id, name, value, scope)
result['data'] = lagoonVariable.addOrUpdateByName(lagoon_project_name, lagoon_environment_name, name, value, scope)
self._display.v("Variable add result: %s" % result['data'])

result['changed'] = True
Expand Down
2 changes: 2 additions & 0 deletions api/plugins/action/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ActionModule(LagoonActionBase):

def run(self, tmp=None, task_vars=None):

self._display.v("Task args: %s" % self._task.args)

if task_vars is None:
task_vars = dict()

Expand Down
20 changes: 10 additions & 10 deletions api/plugins/module_utils/gqlVariable.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ def getForProjects(self, project_names: List[str], fields: List[str] = None) ->

return res

def add(self, type: str, type_id: int, name: str, value: str, scope: str) -> dict:
def addOrUpdateByName(self, projectName:str, environmentName: str, name:str, value:str, scope:str) -> dict:
res = self.client.execute_query(
"""
mutation addEnvVariable(
$type: EnvVariableType!
$type_id: Int!
mutation addOrUpdateEnvVariableByName(
$environment: String
$project: String!
$name: String!
$value: String!
$scope: EnvVariableScope!
) {
addEnvVariable(input: {
type: $type
typeId: $type_id
addOrUpdateEnvVariableByName(input: {
project: $project
environment: $environment
scope: $scope
name: $name
value: $value
Expand All @@ -65,14 +65,14 @@ def add(self, type: str, type_id: int, name: str, value: str, scope: str) -> dic
}
}""",
{
"type": type,
"type_id": int(type_id),
"environment": environmentName,
"project": projectName,
"scope": scope,
"name": name,
"value": str(value),
}
)
return res['addEnvVariable']
return res['addOrUpdateEnvVariableByName']

def delete(self, id: int) -> bool:
res = self.client.execute_query(
Expand Down
16 changes: 8 additions & 8 deletions api/tests/unit/plugins/module_utils/test_gql_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@

class GqlVariableTester(unittest.TestCase):

def test_add(self):
def test_variable_addOrUpdateByName(self):
client = GqlClient('foo', 'bar')
client.execute_query = MagicMock()

lagoonVariable = Variable(client)
lagoonVariable.add('PROJECT', 1, 'SOME_VAR', 'foo', 'RUNTIME')
lagoonVariable.addOrUpdateByName('projectname', "environmentname", 'SOME_VAR', 'foo', 'RUNTIME')
_, query_args = client.execute_query.call_args.args

assert isinstance(query_args['type'], str)
assert query_args['type'] == 'PROJECT'
assert isinstance(query_args['type_id'], int)
assert query_args['type_id'] == 1
assert isinstance(query_args['project'], str)
assert query_args['project'] == 'projectname'
assert isinstance(query_args['environment'], str)
assert query_args['environment'] == 'environmentname'
assert isinstance(query_args['scope'], str)
assert query_args['scope'] == 'RUNTIME'
assert isinstance(query_args['name'], str)
Expand All @@ -35,12 +35,12 @@ def test_add_value_cast_to_string(self):

lagoonVariable = Variable(client)

lagoonVariable.add('PROJECT', 1, 'SOME_VAR', True, 'RUNTIME')
lagoonVariable.addOrUpdateByName('projectname', "environmentname", 'SOME_VAR', True, 'RUNTIME')
_, query_args = client.execute_query.call_args.args
assert isinstance(query_args['value'], str)
assert query_args['value'] == 'True'

lagoonVariable.add('PROJECT', 1, 'SOME_VAR2', 50, 'RUNTIME')
lagoonVariable.addOrUpdateByName('projectname', "environmentname", 'SOME_VAR2', 50, 'RUNTIME')
_, query_args = client.execute_query.call_args.args
assert isinstance(query_args['value'], str)
assert query_args['value'] == '50'
Expand Down
Loading