Skip to content

expiry

Functionality related to asking the user for IAM binding timeframes.

Expiry

Bases: BaseModel

An Expiry denotes a timeframe associated with an IAM binding.

Attributes:

Name Type Description
name str

Friendly name of the timeframe, such as "Until end of today`

timestamp str

ISO8601 timestamp

Source code in dapla_team_cli/tf/iam_bindings/expiry.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Expiry(BaseModel):
    """An `Expiry` denotes a timeframe associated with an IAM binding.

    Attributes:
        name: Friendly name of the timeframe, such as "Until end of today`
        timestamp: ISO8601 timestamp
    """

    name: str
    timestamp: str

    def __lt__(self, __o: object) -> bool:
        """Implement magic __lt__ method to support sorting by name."""
        if not isinstance(__o, Expiry):
            raise NotImplementedError
        return self.name < __o.name

    def __hash__(self) -> int:
        """Implemented to support using GCPRole as a key in a dictionary."""
        return hash((self.name, self.timestamp))

__hash__()

Implemented to support using GCPRole as a key in a dictionary.

Source code in dapla_team_cli/tf/iam_bindings/expiry.py
27
28
29
def __hash__(self) -> int:
    """Implemented to support using GCPRole as a key in a dictionary."""
    return hash((self.name, self.timestamp))

__lt__(__o)

Implement magic lt method to support sorting by name.

Source code in dapla_team_cli/tf/iam_bindings/expiry.py
21
22
23
24
25
def __lt__(self, __o: object) -> bool:
    """Implement magic __lt__ method to support sorting by name."""
    if not isinstance(__o, Expiry):
        raise NotImplementedError
    return self.name < __o.name

ask_for_expiry()

Prompt the user to select a timeframe constraint to be associated with the IAM bindings.

Returns:

Type Description
Expiry

An user selected Expiry to be associated with the IAM bindings.

Source code in dapla_team_cli/tf/iam_bindings/expiry.py
32
33
34
35
36
37
38
39
40
41
42
43
def ask_for_expiry() -> Expiry:
    """Prompt the user to select a timeframe constraint to be associated with the IAM bindings.

    Returns:
        An user selected `Expiry` to be associated with the IAM bindings.
    """
    answer = q.select(
        message="For how long?",
        qmark="📅",
        choices=[q.Choice(item.name, value=item) for item in _expiry_timestamp_items()],
    ).ask()
    return cast(Expiry, answer)

timestamp_at(end_of='', **offset)

Generate a UTC ISO8601 string relative to the current timestamp.

The resulting timestamp can be further specified by supplying additional Pendulum offsets and end_of parameters.

Examples:

hours=2 means the timestamp from now + 2 hours days=4 and end_of="day" means the timestamp from now + 4 days, at the end of that day (23:59:59)

Parameters:

Name Type Description Default
end_of str

Could be any of Pendulum's end_of parameters

''
**offset int

Could be any of Pendulum's parameters

{}

Returns:

Type Description
str

ISO8601 UTC string

Source code in dapla_team_cli/tf/iam_bindings/expiry.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def timestamp_at(end_of: str = "", **offset: int) -> str:
    """Generate a UTC ISO8601 string relative to the current timestamp.

    The resulting timestamp can be further specified by supplying additional Pendulum offsets and `end_of` parameters.

    Examples:
        `hours=2` means the timestamp from now + 2 hours
        `days=4` and `end_of="day"` means the timestamp from now + 4 days, at the end of that day (23:59:59)

    Args:
        end_of: Could be any of Pendulum's `end_of` parameters
        **offset: Could be any of Pendulum's parameters

    Returns:
        ISO8601 UTC string
    """
    timestamp = pendulum.now("UTC")
    if offset:
        timestamp = timestamp.add(**offset)
    if end_of:
        timestamp = timestamp.end_of(end_of)

    timestamp_str = str(timestamp.to_iso8601_string())
    if len(timestamp_str) > 25:
        # Remove microseconds and milliseconds
        timestamp_str = timestamp_str[:19] + timestamp_str[26:]
    return timestamp_str