|
| 1 | +package ftl.environment |
| 2 | + |
| 3 | +import com.google.api.services.testing.model.NetworkConfiguration |
| 4 | +import com.google.api.services.testing.model.TrafficRule |
| 5 | + |
| 6 | +fun networkProfileDescription(profile: String) = getNetworkConfiguration() |
| 7 | + .find { it.id?.toUpperCase() == profile.toUpperCase() } |
| 8 | + .toNullProof() |
| 9 | + .prepareDescription() |
| 10 | + ?: "Unable to fetch profile [$profile] description" |
| 11 | + |
| 12 | +private fun NetworkConfiguration?.toNullProof() = this?.run { |
| 13 | + NetworkConfigurationWrapper( |
| 14 | + downRule = wrappedOrEmpty(downRule), |
| 15 | + upRule = wrappedOrEmpty(upRule), |
| 16 | + id = id.toStringOrUnableToFetch() |
| 17 | + ) |
| 18 | +} |
| 19 | + |
| 20 | +private fun wrappedOrEmpty(rule: TrafficRule?) = rule?.let { |
| 21 | + Rule( |
| 22 | + bandwidth = it.bandwidth.toStringOrUnableToFetch(), |
| 23 | + delay = it.delay.toStringOrUnableToFetch(), |
| 24 | + packetLossRatio = it.packetLossRatio.toStringOrUnableToFetch() |
| 25 | + ) |
| 26 | +} ?: emptyRule |
| 27 | + |
| 28 | +private const val UNABLE = "[Unable to fetch]" |
| 29 | + |
| 30 | +private fun Any?.toStringOrUnableToFetch() = this?.toString() ?: UNABLE |
| 31 | + |
| 32 | +private val emptyRule: Rule |
| 33 | + get() = Rule(UNABLE, UNABLE, UNABLE) |
| 34 | + |
| 35 | +private fun NetworkConfigurationWrapper?.prepareDescription() = this?.run { |
| 36 | + """ |
| 37 | +downRule: |
| 38 | + bandwidth: ${downRule.bandwidth} |
| 39 | + delay: ${downRule.delay} |
| 40 | + packetLossRatio: ${downRule.packetLossRatio} |
| 41 | +id: $id |
| 42 | +upRule: |
| 43 | + bandwidth: ${upRule.bandwidth} |
| 44 | + delay: ${upRule.delay} |
| 45 | + packetLossRatio: ${upRule.packetLossRatio} |
| 46 | +""".trimIndent() |
| 47 | +} |
| 48 | + |
| 49 | +private data class NetworkConfigurationWrapper(val downRule: Rule, val upRule: Rule, val id: String) |
| 50 | + |
| 51 | +private data class Rule(val bandwidth: String, val delay: String, val packetLossRatio: String) |
0 commit comments