|
| 1 | +package com.linkedin.datahub.graphql.resolvers.structuredproperties; |
| 2 | + |
| 3 | +import static com.linkedin.datahub.graphql.resolvers.ResolverUtils.bindArgument; |
| 4 | +import static com.linkedin.metadata.Constants.STRUCTURED_PROPERTIES_ASPECT_NAME; |
| 5 | + |
| 6 | +import com.datahub.authentication.Authentication; |
| 7 | +import com.linkedin.common.AuditStamp; |
| 8 | +import com.linkedin.common.urn.Urn; |
| 9 | +import com.linkedin.common.urn.UrnUtils; |
| 10 | +import com.linkedin.datahub.graphql.QueryContext; |
| 11 | +import com.linkedin.datahub.graphql.exception.AuthorizationException; |
| 12 | +import com.linkedin.datahub.graphql.generated.PropertyValueInput; |
| 13 | +import com.linkedin.datahub.graphql.generated.UpsertStructuredPropertiesInput; |
| 14 | +import com.linkedin.datahub.graphql.resolvers.mutate.util.StructuredPropertyUtils; |
| 15 | +import com.linkedin.datahub.graphql.types.structuredproperty.StructuredPropertiesMapper; |
| 16 | +import com.linkedin.entity.EntityResponse; |
| 17 | +import com.linkedin.entity.client.EntityClient; |
| 18 | +import com.linkedin.metadata.entity.AspectUtils; |
| 19 | +import com.linkedin.metadata.utils.AuditStampUtils; |
| 20 | +import com.linkedin.mxe.MetadataChangeProposal; |
| 21 | +import com.linkedin.structured.PrimitivePropertyValueArray; |
| 22 | +import com.linkedin.structured.StructuredProperties; |
| 23 | +import com.linkedin.structured.StructuredPropertyValueAssignment; |
| 24 | +import com.linkedin.structured.StructuredPropertyValueAssignmentArray; |
| 25 | +import graphql.com.google.common.collect.ImmutableSet; |
| 26 | +import graphql.schema.DataFetcher; |
| 27 | +import graphql.schema.DataFetchingEnvironment; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Objects; |
| 32 | +import java.util.concurrent.CompletableFuture; |
| 33 | +import java.util.stream.Collectors; |
| 34 | +import javax.annotation.Nonnull; |
| 35 | + |
| 36 | +public class UpsertStructuredPropertiesResolver |
| 37 | + implements DataFetcher< |
| 38 | + CompletableFuture<com.linkedin.datahub.graphql.generated.StructuredProperties>> { |
| 39 | + |
| 40 | + private final EntityClient _entityClient; |
| 41 | + |
| 42 | + public UpsertStructuredPropertiesResolver(@Nonnull final EntityClient entityClient) { |
| 43 | + _entityClient = Objects.requireNonNull(entityClient, "entityClient must not be null"); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public CompletableFuture<com.linkedin.datahub.graphql.generated.StructuredProperties> get( |
| 48 | + final DataFetchingEnvironment environment) throws Exception { |
| 49 | + final QueryContext context = environment.getContext(); |
| 50 | + final Authentication authentication = context.getAuthentication(); |
| 51 | + |
| 52 | + final UpsertStructuredPropertiesInput input = |
| 53 | + bindArgument(environment.getArgument("input"), UpsertStructuredPropertiesInput.class); |
| 54 | + final Urn assetUrn = UrnUtils.getUrn(input.getAssetUrn()); |
| 55 | + Map<String, List<PropertyValueInput>> updateMap = new HashMap<>(); |
| 56 | + // create a map of updates from our input |
| 57 | + input |
| 58 | + .getStructuredPropertyInputParams() |
| 59 | + .forEach(param -> updateMap.put(param.getStructuredPropertyUrn(), param.getValues())); |
| 60 | + |
| 61 | + return CompletableFuture.supplyAsync( |
| 62 | + () -> { |
| 63 | + try { |
| 64 | + // check authorization first |
| 65 | + if (!StructuredPropertyUtils.isAuthorizedToUpdateProperties(context, assetUrn)) { |
| 66 | + throw new AuthorizationException( |
| 67 | + String.format( |
| 68 | + "Not authorized to update properties on the gives urn %s", assetUrn)); |
| 69 | + } |
| 70 | + |
| 71 | + final AuditStamp auditStamp = |
| 72 | + AuditStampUtils.createAuditStamp(authentication.getActor().toUrnStr()); |
| 73 | + |
| 74 | + if (!_entityClient.exists(assetUrn, authentication)) { |
| 75 | + throw new RuntimeException( |
| 76 | + String.format("Asset with provided urn %s does not exist", assetUrn)); |
| 77 | + } |
| 78 | + |
| 79 | + // get or default the structured properties aspect |
| 80 | + StructuredProperties structuredProperties = |
| 81 | + getStructuredProperties(assetUrn, authentication); |
| 82 | + |
| 83 | + // update the existing properties based on new value |
| 84 | + StructuredPropertyValueAssignmentArray properties = |
| 85 | + updateExistingProperties(structuredProperties, updateMap, auditStamp); |
| 86 | + |
| 87 | + // append any new properties from our input |
| 88 | + addNewProperties(properties, updateMap, auditStamp); |
| 89 | + |
| 90 | + structuredProperties.setProperties(properties); |
| 91 | + |
| 92 | + // ingest change proposal |
| 93 | + final MetadataChangeProposal structuredPropertiesProposal = |
| 94 | + AspectUtils.buildMetadataChangeProposal( |
| 95 | + assetUrn, STRUCTURED_PROPERTIES_ASPECT_NAME, structuredProperties); |
| 96 | + |
| 97 | + _entityClient.ingestProposal(structuredPropertiesProposal, authentication, false); |
| 98 | + |
| 99 | + return StructuredPropertiesMapper.map(structuredProperties); |
| 100 | + } catch (Exception e) { |
| 101 | + throw new RuntimeException( |
| 102 | + String.format("Failed to perform update against input %s", input), e); |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + private StructuredProperties getStructuredProperties(Urn assetUrn, Authentication authentication) |
| 108 | + throws Exception { |
| 109 | + EntityResponse response = |
| 110 | + _entityClient.getV2( |
| 111 | + assetUrn.getEntityType(), |
| 112 | + assetUrn, |
| 113 | + ImmutableSet.of(STRUCTURED_PROPERTIES_ASPECT_NAME), |
| 114 | + authentication); |
| 115 | + StructuredProperties structuredProperties = new StructuredProperties(); |
| 116 | + structuredProperties.setProperties(new StructuredPropertyValueAssignmentArray()); |
| 117 | + if (response != null && response.getAspects().containsKey(STRUCTURED_PROPERTIES_ASPECT_NAME)) { |
| 118 | + structuredProperties = |
| 119 | + new StructuredProperties( |
| 120 | + response.getAspects().get(STRUCTURED_PROPERTIES_ASPECT_NAME).getValue().data()); |
| 121 | + } |
| 122 | + return structuredProperties; |
| 123 | + } |
| 124 | + |
| 125 | + private StructuredPropertyValueAssignmentArray updateExistingProperties( |
| 126 | + StructuredProperties structuredProperties, |
| 127 | + Map<String, List<PropertyValueInput>> updateMap, |
| 128 | + AuditStamp auditStamp) { |
| 129 | + return new StructuredPropertyValueAssignmentArray( |
| 130 | + structuredProperties.getProperties().stream() |
| 131 | + .map( |
| 132 | + propAssignment -> { |
| 133 | + String propUrnString = propAssignment.getPropertyUrn().toString(); |
| 134 | + if (updateMap.containsKey(propUrnString)) { |
| 135 | + List<PropertyValueInput> valueList = updateMap.get(propUrnString); |
| 136 | + PrimitivePropertyValueArray values = |
| 137 | + new PrimitivePropertyValueArray( |
| 138 | + valueList.stream() |
| 139 | + .map(StructuredPropertyUtils::mapPropertyValueInput) |
| 140 | + .collect(Collectors.toList())); |
| 141 | + propAssignment.setValues(values); |
| 142 | + propAssignment.setLastModified(auditStamp); |
| 143 | + } |
| 144 | + return propAssignment; |
| 145 | + }) |
| 146 | + .collect(Collectors.toList())); |
| 147 | + } |
| 148 | + |
| 149 | + private void addNewProperties( |
| 150 | + StructuredPropertyValueAssignmentArray properties, |
| 151 | + Map<String, List<PropertyValueInput>> updateMap, |
| 152 | + AuditStamp auditStamp) { |
| 153 | + // first remove existing properties from updateMap so that we append only new properties |
| 154 | + properties.forEach(prop -> updateMap.remove(prop.getPropertyUrn().toString())); |
| 155 | + |
| 156 | + updateMap.forEach( |
| 157 | + (structuredPropUrn, values) -> { |
| 158 | + StructuredPropertyValueAssignment valueAssignment = |
| 159 | + new StructuredPropertyValueAssignment(); |
| 160 | + valueAssignment.setPropertyUrn(UrnUtils.getUrn(structuredPropUrn)); |
| 161 | + valueAssignment.setValues( |
| 162 | + new PrimitivePropertyValueArray( |
| 163 | + values.stream() |
| 164 | + .map(StructuredPropertyUtils::mapPropertyValueInput) |
| 165 | + .collect(Collectors.toList()))); |
| 166 | + valueAssignment.setCreated(auditStamp); |
| 167 | + valueAssignment.setLastModified(auditStamp); |
| 168 | + properties.add(valueAssignment); |
| 169 | + }); |
| 170 | + } |
| 171 | +} |
0 commit comments