-
Notifications
You must be signed in to change notification settings - Fork 1.4k
core: introduce a set of type adapters #13624
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
Draft
weizhouapache
wants to merge
6
commits into
apache:4.22
Choose a base branch
from
shapeblue:4.22-type_adapters
base: 4.22
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+351
−1
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
620d389
There is a set of TO classes with renamed fields, which makes impossi…
mprokopchuk e8c2037
fix logger
weizhouapache 44ddaef
Apply suggestion from Daman
weizhouapache 15b39f4
Apply suggestion from Daman 2
weizhouapache 8a811d5
Fix nested TO renaming, log redaction and cleanup in compat TypeAdaptors
Damans227 5efe316
fix logger
weizhouapache File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
core/src/main/java/com/cloud/agent/transport/compat/AbstractTOAdaptor.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package com.cloud.agent.transport.compat; | ||
|
|
||
| import com.cloud.utils.StringUtils; | ||
| import com.cloud.utils.exception.CloudRuntimeException; | ||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonSerializationContext; | ||
| import com.google.gson.JsonSerializer; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * JSON serializer adapter for transport classes (com.cloud.agent.api.to.*) that ensures backward compatibility | ||
| * with older Agent versions due to rename of the fields | ||
| * (see https://github.com/apache/cloudstack/pull/10514) | ||
| * | ||
| * This class does not build its own Gson instance: doing so would silently drop whichever exclusion | ||
| * strategy (e.g. log redaction) and sibling compat adaptors (for nested TOs) the enclosing Gson was | ||
| * configured with. Instead, whoever registers an instance of this class into a GsonBuilder is | ||
| * responsible for also calling {@link #initGson(Gson)} with a Gson that (a) carries that same | ||
| * exclusion strategy and (b) has adapters registered for any nested TO types that also need field | ||
| * renaming, but not for this adaptor's own type (to avoid infinite recursion). See | ||
| * {@link com.cloud.serializer.GsonHelper#setDefaultGsonConfig(com.google.gson.GsonBuilder)}. | ||
| */ | ||
| public class AbstractTOAdaptor<T> implements JsonSerializer<T> { | ||
| private Gson gson; | ||
| private Map<String, String> fieldMappings; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be |
||
|
|
||
| protected AbstractTOAdaptor(String... fields) { | ||
| this.fieldMappings = new LinkedHashMap<>(); | ||
| for (int i = 0; i + 1 < fields.length; i += 2) { | ||
| String sourceField = fields[i]; | ||
| String destinationField = fields[i + 1]; | ||
| // skip empty fields | ||
| if (StringUtils.isBlank(sourceField) || StringUtils.isBlank(destinationField)) { | ||
| continue; | ||
| } | ||
| this.fieldMappings.put(sourceField, destinationField); | ||
| } | ||
| if (this.fieldMappings.isEmpty()) { | ||
| throw new CloudRuntimeException("Field mappings must not be empty"); | ||
| } | ||
| } | ||
|
|
||
| public void initGson(Gson gson) { | ||
| this.gson = gson; | ||
| } | ||
|
|
||
| @Override | ||
| public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) { | ||
| if (src == null) { | ||
| return null; | ||
| } | ||
| JsonElement tree = gson.toJsonTree(src); | ||
| if (tree.isJsonObject()) { | ||
| JsonObject obj = tree.getAsJsonObject(); | ||
| for (Map.Entry<String, String> field : fieldMappings.entrySet()) { | ||
| String sourceField = field.getKey(); | ||
| String destinationField = field.getValue(); | ||
| if (obj.has(sourceField)) { | ||
| obj.add(destinationField, obj.get(sourceField)); | ||
| } | ||
| } | ||
| } | ||
| return tree; | ||
| } | ||
| } | ||
28 changes: 28 additions & 0 deletions
28
core/src/main/java/com/cloud/agent/transport/compat/DiskTOAdaptor.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package com.cloud.agent.transport.compat; | ||
|
|
||
| import com.cloud.agent.api.to.DiskTO; | ||
|
|
||
| /** | ||
| * See {@link AbstractTOAdaptor}. | ||
| */ | ||
| public class DiskTOAdaptor extends AbstractTOAdaptor<DiskTO> { | ||
| public DiskTOAdaptor() { | ||
| super("details", "_details"); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
core/src/main/java/com/cloud/agent/transport/compat/MigrateCommandAdaptor.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package com.cloud.agent.transport.compat; | ||
|
|
||
| import com.cloud.agent.api.MigrateCommand; | ||
|
|
||
| /** | ||
| * See {@link AbstractTOAdaptor}. | ||
| */ | ||
| public class MigrateCommandAdaptor extends AbstractTOAdaptor<MigrateCommand> { | ||
| public MigrateCommandAdaptor() { | ||
| super("destinationIp", "destIp", "windows", "isWindows", "virtualMachine", "vmTO"); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
core/src/main/java/com/cloud/agent/transport/compat/NetworkTOAdaptor.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package com.cloud.agent.transport.compat; | ||
|
|
||
| import com.cloud.agent.api.to.NetworkTO; | ||
|
|
||
| /** | ||
| * See {@link AbstractTOAdaptor}. | ||
| */ | ||
| public class NetworkTOAdaptor extends AbstractTOAdaptor<NetworkTO> { | ||
| public NetworkTOAdaptor() { | ||
| super("securityGroupEnabled", "isSecurityGroupEnabled"); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
core/src/main/java/com/cloud/agent/transport/compat/VirtualMachineTOAdaptor.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package com.cloud.agent.transport.compat; | ||
|
|
||
| import com.cloud.agent.api.to.VirtualMachineTO; | ||
|
|
||
| /** | ||
| * See {@link AbstractTOAdaptor}. | ||
| */ | ||
| public class VirtualMachineTOAdaptor extends AbstractTOAdaptor<VirtualMachineTO> { | ||
|
|
||
| public VirtualMachineTOAdaptor() { | ||
| super("details", "params"); | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.