MCPcopy Create free account
hub / github.com/beelit94/python-terraform / cmd

Method cmd

python_terraform/terraform.py:286–367  ·  view source on GitHub ↗

Run a terraform command, if success, will try to read state file :param cmd: command and sub-command of terraform, seperated with space refer to https://www.terraform.io/docs/commands/index.html :param args: arguments of a command :param kwargs: any opti

(
        self,
        cmd: str,
        *args,
        capture_output: Union[bool, str] = True,
        raise_on_error: bool = True,
        synchronous: bool = True,
        **kwargs,
    )

Source from the content-addressed store, hash-verified

284 return cmds
285
286 def cmd(
287 self,
288 cmd: str,
289 *args,
290 capture_output: Union[bool, str] = True,
291 raise_on_error: bool = True,
292 synchronous: bool = True,
293 **kwargs,
294 ) -> CommandOutput:
295 """Run a terraform command, if success, will try to read state file
296
297 :param cmd: command and sub-command of terraform, seperated with space
298 refer to https://www.terraform.io/docs/commands/index.html
299 :param args: arguments of a command
300 :param kwargs: any option flag with key value without prefixed dash character
301 if there's a dash in the option name, use under line instead of dash,
302 ex. -no-color --> no_color
303 if it's a simple flag with no value, value should be IsFlagged
304 ex. cmd('taint', allow_missing=IsFlagged)
305 if it's a boolean value flag, assign True or false
306 if it's a flag could be used multiple times, assign list to it's value
307 if it's a "var" variable flag, assign dictionary to it
308 if a value is None, will skip this option
309 if the option 'capture_output' is passed (with any value other than
310 True), terraform output will be printed to stdout/stderr and
311 "None" will be returned as out and err.
312 if the option 'raise_on_error' is passed (with any value that evaluates to True),
313 and the terraform command returns a nonzerop return code, then
314 a TerraformCommandError exception will be raised. The exception object will
315 have the following properties:
316 returncode: The command's return code
317 out: The captured stdout, or None if not captured
318 err: The captured stderr, or None if not captured
319 :return: ret_code, out, err
320 """
321 if capture_output is True:
322 stderr = subprocess.PIPE
323 stdout = subprocess.PIPE
324 elif capture_output == "framework":
325 stderr = None
326 stdout = None
327 else:
328 stderr = sys.stderr
329 stdout = sys.stdout
330
331 cmds = self.generate_cmd_string(cmd, *args, **kwargs)
332 logger.info("Command: %s", " ".join(cmds))
333
334 working_folder = self.working_dir if self.working_dir else None
335
336 environ_vars = {}
337 if self.is_env_vars_included:
338 environ_vars = os.environ.copy()
339
340 p = subprocess.Popen(
341 cmds, stdout=stdout, stderr=stderr, cwd=working_folder, env=environ_vars
342 )
343

Callers 10

wrapperMethod · 0.95
applyMethod · 0.95
destroyMethod · 0.95
planMethod · 0.95
initMethod · 0.95
set_workspaceMethod · 0.95
create_workspaceMethod · 0.95
delete_workspaceMethod · 0.95
show_workspaceMethod · 0.95
test_terraform.pyFile · 0.80

Calls 4

generate_cmd_stringMethod · 0.95
read_state_fileMethod · 0.95
clean_upMethod · 0.80

Tested by

no test coverage detected